Hiding Default Store View Code from URL in Magento
In Magento, by default, the store code is appended to the URL for multi-store setups. However, removing it entirely can be complex without affecting other stores or breaking URLs. Here’s how you can achieve this specifically for your default French store.
The Challenge with Magento’s URL Configuration
Magento provides a simple toggle under System -> Configuration -> Web -> Add Store Codes to URLs. Enabling this appends the store code to all URLs. Disabling it removes them entirely for all stores, which isn’t ideal if you need mixed structures like in your case.
A Targeted Solution: Override Core Functionality
To solve this, we’ll override a core Magento function that generates URLs. This approach allows us to maintain control over the default store’s URL structure while leaving other stores intact.
Here are the steps:
- Create an extension configuration file
First, create a new configuration file in your custom theme or module directory. The path should be app/code/[VendorName]/[ModuleName]/etc/adminhtml/system.xml.
- Add the system config settings
Open the newly created XML file and add these lines inside the <config> tags:
<config>
<sections>
<my_module translate="label" module="my_module">
<label>My Module</label>
<frontend_type>textfield</frontend_type>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</my_module>
</sections>
<fields>
<general>
<translate enabled="true"/>
<my_module>
<label>Hide Default Store Code</label>
<frontend_type>textfield</frontend_type>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</my_module>
</general>
</fields>
</config>
- Override the Mage_Core_Model_Url::getUrl method
Create a new file at app/code/[VendorName]/[ModuleName]/etc/modules.xml and add:
<config>
<modules>
<MyCompany_HideStoreCode>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Core />
</depends>
</MyCompany_HideStoreCode>
</modules>
</config>
- Create a rewrite of getUrl method
In your custom module directory, create app/code/[VendorName]/[ModuleName]/etc/rewrites.xml and add:
<config>
<rewrite>
<Mage_Core_Model_Url_getUrl>
<from><![CDATA[#Mage_Core_Model_Url::getUrl]]></from>
<to><![CDATA[MyCompany_HideStoreCode_Model_Url::getUrl]]></to>
</Mage_Core_Model_Url_getUrl>
</rewrite>
</config>
- Implement the rewrite
Create app/code/[VendorName]/[ModuleName]/Model/Url.php and add:
class MyCompany_HideStoreCode_Model_Url extends Mage_Core_Model_Url
{
public function getUrl($route = '', $params = array())
{
if ($this->_store->getId() == 0 && empty($params['_nosid'])) { // Assuming your default store ID is 0
unset($params['_store']);
}
return parent::getUrl($route, $params);
}
}
Conclusion
By following this approach, you’re able to maintain granular control over the URL structure for each store. The default French store’s URLs will no longer include the language code while other stores remain unaffected.
This method requires some core function overriding but it provides a clean, targeted solution that doesn’t require global changes or complex rewrite rules.
For more advanced Magento customizations and best practices, feel free to explore our Magento developer resources at LaravelCompany.