Magento provides configuration settings that control how links are generated throughout a store. When working with multiple stores, custom installation paths, or Magento setup scripts, you may need to set the store's base link URL programmatically instead of updating it manually from the administration panel.
In this guide, we'll show you how to set the web/unsecure/base_link_url and web/secure/base_link_url values programmatically using Magento's setup installer. We'll also explain the difference between a base URL and a base link URL, how store scope works, common mistakes, and how to troubleshoot URL-related problems.
Set Magento Base Link URL Programmatically
The following code can be used in a Magento setup or installation script to configure the base link URL for a specific store:
$installer->setConfigData( 'web/unsecure/base_link_url', '{{unsecure_base_url}}mystore/', 'stores', $storeId);$installer->setConfigData( 'web/secure/base_link_url', '{{secure_base_url}}mystore/', 'stores', $storeId);
The code sets both the unsecure and secure base link URLs at the store scope. The $storeId variable identifies the store whose configuration should be updated.
What Is a Magento Base Link URL?
The base link URL is a Magento configuration value used as a placeholder when generating links relative to the store's base URL. Magento provides separate values for unsecure and secure URLs.
The two configuration paths used in the example are:
web/unsecure/base_link_urlweb/secure/base_link_url
The corresponding placeholders are:
{{unsecure_base_url}}{{secure_base_url}}
These placeholders allow Magento to build links relative to the configured base URL rather than hard-coding the complete domain into every link.
Base URL vs Base Link URL in Magento
It is important to understand the difference between base_url and base_link_url because they serve different purposes.
Base URL
The base URL represents the actual URL of the Magento installation. For example:
The relevant configuration paths are:
web/unsecure/base_urlweb/secure/base_url
Base Link URL
The base link URL is generally a placeholder-based value used when Magento generates links:
{{unsecure_base_url}}mystore/{{secure_base_url}}mystore/
This distinction is important when modifying Magento URL configuration programmatically. Changing the base link URL does not necessarily change the actual domain configured as the store's base URL.
Understanding the Magento setConfigData() Method
The setConfigData() method allows configuration values to be written for a particular scope during a Magento setup process.
The basic structure is:
$installer->setConfigData( $path, $value, $scope, $scopeId);
In our example:
$installer->setConfigData( 'web/unsecure/base_link_url', '{{unsecure_base_url}}mystore/', 'stores', $storeId);
Each parameter has a specific purpose:
- $path — The Magento configuration path.
- $value — The value that should be stored.
- $scope — The configuration scope, such as
stores. - $scopeId — The ID of the store that should receive the configuration.
Why Specify the Store ID?
Magento can support multiple stores and store views within the same installation. Therefore, a configuration change may need to apply only to a particular store instead of affecting the entire installation.
Using:
'stores', $storeId
tells Magento that the configuration should be saved at the store level for the specified store ID.
This is especially useful when creating a setup script that needs to configure several stores independently.
Set Base Link URL for Multiple Magento Stores
If your Magento installation contains multiple stores, you can loop through the stores and apply different configuration values based on each store's ID.
$stores = Mage::app()->getStores();foreach ($stores as $storeId => $store) { $installer->setConfigData( 'web/unsecure/base_link_url', '{{unsecure_base_url}}mystore/', 'stores', $storeId ); $installer->setConfigData( 'web/secure/base_link_url', '{{secure_base_url}}mystore/', 'stores', $storeId );}
This approach can be useful when a Magento installation contains multiple storefronts that share the same application but require store-specific configuration.
Using a Dynamic Store Path
If each store has a different path, you can construct the value dynamically instead of hard-coding mystore/.
$storePath = 'store-' . $storeId . '/';$installer->setConfigData( 'web/unsecure/base_link_url', '{{unsecure_base_url}}' . $storePath, 'stores', $storeId);$installer->setConfigData( 'web/secure/base_link_url', '{{secure_base_url}}' . $storePath, 'stores', $storeId);
This allows the setup script to generate store-specific paths automatically.
Secure and Unsecure Base Link URLs
Magento maintains separate configuration paths for HTTP and HTTPS URLs.
web/unsecure/base_link_urlweb/secure/base_link_url
If your storefront supports HTTPS, it is important to configure the secure value correctly. A mismatch between secure URL settings and the actual HTTPS configuration can result in redirects, broken links, or redirect loops.
For a secure Magento storefront, the secure configuration should ultimately resolve to an HTTPS base URL.
Base Link URL with a Magento Subdirectory
If Magento is installed inside a subdirectory, the URL may contain an additional path.
For example:
If the store is located inside another directory, the base link configuration may need to reflect that structure:
{{secure_base_url}}mystore/
Always make sure the resulting URL has the correct path and trailing slash for your Magento installation.
Where Magento Stores URL Configuration
Magento's web URL configuration uses paths such as:
web/unsecure/base_urlweb/unsecure/base_link_urlweb/secure/base_urlweb/secure/base_link_url
In Magento installations that use the core_config_data configuration table, these values can be inspected to troubleshoot URL configuration issues.
For example, you can check the relevant configuration records with:
SELECT *FROM core_config_dataWHERE path IN ( 'web/unsecure/base_url', 'web/unsecure/base_link_url', 'web/secure/base_url', 'web/secure/base_link_url');
When working on a production store, always back up the database before making direct database changes.
Common Problems When Setting Magento Base Link URLs
1. Using the Wrong Configuration Path
One common mistake is confusing base_url with base_link_url.
For example:
web/unsecure/base_urlweb/unsecure/base_link_url
These are different configuration values. Make sure you are updating the setting required by your specific use case.
2. Using the Wrong Store ID
If the configuration is written for the wrong store ID, the expected storefront may not reflect the change.
Verify the store ID before running the setup script.
3. Forgetting the Trailing Slash
Magento base URLs conventionally use a trailing slash. A missing slash can result in incorrectly constructed URLs when additional paths are appended.
For example:
is preferable to:
4. Incorrect HTTP and HTTPS Configuration
Make sure the unsecure and secure configuration values correspond to the protocols actually supported by the store.
5. Configuration Cache
After changing Magento configuration, cached configuration data can sometimes prevent changes from appearing immediately. Clear the appropriate Magento cache before testing the updated URLs.
How to Verify the Magento Configuration
After running your setup script, verify that the expected configuration has been stored for the correct store.
You can inspect the configuration from the Magento administration area or query the configuration table in environments where that approach is appropriate.
SELECT scope, scope_id, path, valueFROM core_config_dataWHERE path LIKE 'web/%/base%url%';
Check that the scope, store ID, configuration path, and value are all correct.
When Should You Set Magento URLs Programmatically?
Programmatic configuration is particularly useful when the value needs to be applied automatically as part of a repeatable deployment or setup process.
Common scenarios include:
- Creating a Magento setup script
- Installing a custom Magento module
- Configuring multiple stores
- Creating development or staging environments
- Moving a Magento installation
- Automating store configuration
- Adding a store-specific subdirectory
- Standardizing configuration across environments
Best Practices for Magento URL Configuration
- Always verify the target store ID before updating store-scoped configuration.
- Keep secure and unsecure URL settings consistent with your server configuration.
- Use HTTPS for production storefronts whenever possible.
- Include the correct trailing slash in base URL values.
- Avoid hard-coding environment-specific domains in reusable deployment scripts when a configurable value can be used.
- Clear the appropriate configuration cache after making configuration changes.
- Back up your database before making direct configuration-table changes.
- Test storefront, category, product, checkout, and customer links after changing URL configuration.
Frequently Asked Questions
How do I set Magento's base link URL programmatically?
You can use Magento's setConfigData() method in a setup script:
$installer->setConfigData( 'web/unsecure/base_link_url', '{{unsecure_base_url}}mystore/', 'stores', $storeId);
What is the Magento base_link_url configuration path?
The unsecure base link URL uses web/unsecure/base_link_url, while the secure base link URL uses web/secure/base_link_url.
What is the difference between base_url and base_link_url in Magento?
base_url represents the actual base address of the Magento installation, while base_link_url is a placeholder-based configuration value used when Magento generates links relative to the base URL.
How do I set the base link URL for a specific Magento store?
Pass the store scope and store ID to setConfigData():
$installer->setConfigData( 'web/unsecure/base_link_url', '{{unsecure_base_url}}mystore/', 'stores', $storeId);
Can I configure Magento base URLs for multiple stores?
Yes. You can retrieve the available stores and loop through them, applying the appropriate configuration values to each store ID.
Should the Magento base URL end with a slash?
Yes. Magento base URL values should normally include a trailing slash so that additional URL paths can be appended correctly.
Why are Magento URLs still showing the old value after changing the configuration?
Cached configuration can cause old values to remain active. Clear the appropriate Magento cache and then test the storefront again. Also verify that the configuration was saved at the correct scope and store ID.
Can I use this code in Magento 2?
This example uses the Magento 1-style setup installer API and should not be copied directly into a Magento 2 module. Magento 2 uses a different setup and configuration architecture, so Magento 2 projects require a version-appropriate implementation.
Can an incorrect secure base URL cause redirect problems?
Yes. If secure URL settings point to an incorrect protocol or URL, Magento can produce unexpected redirects or redirect loops. Always verify that the secure configuration resolves to the correct HTTPS address when SSL is enabled.
Conclusion
Setting Magento's base link URL programmatically can save time when configuring stores through setup scripts, automating deployments, or managing a multi-store Magento installation.
The key configuration values are web/unsecure/base_link_url and web/secure/base_link_url. By using setConfigData() with the appropriate store scope and store ID, you can apply the configuration without manually changing every store from the administration panel.
Before deploying URL changes to production, verify the store ID, URL structure, HTTP/HTTPS configuration, trailing slash, and Magento cache. Testing generated links after the change can help prevent broken URLs and redirect-related problems.
Need Help With Magento Development?
Managing a Magento store and need help with custom development, store configuration, Magento upgrades, performance optimization, API integrations, or ongoing maintenance?
Contact SCRIPTBAKER to discuss your Magento development requirements and get help building, modernizing, or maintaining your eCommerce platform.