Magento - Change store base url programatically

Magento - Change store base url programatically

September 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Change store base url programatically

Programmatically Changing Base URLs in Magento

As an experienced developer and technical blogger, I often encounter questions like this from fellow developers looking to streamline their workflows within the Magento platform. Let’s dive into how you can change your store base URLs (secure and unsecure) programmatically.

Understanding the Current Manual Process

Firstly, let me briefly recap the manual process you’re already familiar with. The Magento admin provides a user-friendly interface for changing these URLs under System > Configuration > General > Web. Here’s what that looks like:

Magento Base URL Settings

The fields you’re interested in are located at positions 4 and 5—specifically, Base URL (Secure) and **Base URL (Unsecure) for your chosen store view.

Changing Base URLs Programmatically

Now, let’s explore how to achieve this programmatically using the Magento API. You’ve already started on the right track by accessing the core/store model:

$store = Mage::getModel('core/store')->load($storeId);

However, the next part is where we need to make a slight adjustment to your code snippet. To set the base URLs for a store programmatically, you’ll want to use the appropriate fields provided by the Mage_Core_Model_Store class.

Here’s how you can do it:

$store = Mage::getModel('core/store')->load($storeId);
$store->setConfig('web/secure/base_url', 'xyz.com');
$store->setConfig('web/unsecure/base_url', 'xyz.com');
$store->save();

In this example, we’re setting both the secure and unsecure base URLs to xyz.com for your chosen store. The setConfig method allows us to directly modify these configuration values at runtime.

Best Practices and Additional Notes

A few best practices to keep in mind:

  1. Ensure Store Existence: Before proceeding, always check if the desired store exists using $store->getId() > 0.

  2. Handle Exception Cases: Wrap your code in a try-catch block to handle any potential exceptions that might arise during the save operation.

  3. Flush Cache: After making changes to core configuration values, remember to flush the cache using Mage::app()->cleanCache().

Conclusion

As you can see, changing your Magento store base URLs programmatically is a straightforward process once you understand how to interact with the appropriate models and methods provided by the framework.

Remember, working with the Magento API allows for greater flexibility and automation in managing your stores’ configuration. By incorporating this technique into your development workflow, you can save time and eliminate manual steps whenever you need to update these critical settings across multiple stores or environments.

If you’re looking to learn more about Magento API best practices or seeking advanced solutions to streamline your e-commerce operations, feel free to explore the comprehensive guides available on Laravel Company.

API Admin Configuration

Our network