Multi-Store URL Rewrites in Magento: A Comprehensive Guide
Dear Reader,
We understand that managing multiple stores within a single Magento installation can be challenging, especially when it comes to configuring URLs for each storefront. In this post, we’ll explore the common issues faced while implementing multi-store URL rewrites and provide a step-by-step solution using a combination of server configuration and Magento settings.
Understanding the Problem: Multiple Store Codes in URLs
When you enable “Add Store Code to Urls” in System > Configuration > Web, Magento attempts to add each store’s unique code to its respective URLs. However, this process is not flawless and can lead to duplicate store codes or incorrect base store names being appended to certain URLs.
For example:
- One store may have its name repeated twice (e.g.,
https://example.com/sitename-sitename/) - Other stores might show the base store’s code instead of their own
This inconsistency can impact SEO, navigation, and user experience across your multi-store setup.
Attempted Solutions: Configuring .htaccess Files
You’ve already experimented with adding environment variables to the .htaccess file in the public_html folder. While this approach works for some stores (e.g., site_ahepa), it fails to recognize other store codes and appends incorrect or duplicate store names instead.
The issue arises because the regular expressions used in your example are not matching the actual request URIs correctly:
SetEnvIf Request_URI ^\/site_wow\/ MAGE_RUN_CODE=site_wow
To resolve this, we need to re-evaluate our approach and combine it with Magento’s built-in multi-store functionality.
The Optimal Solution: Combining Server and Magento Settings
Step 1: Enable “Add Store Code to Urls”
In your Magento admin panel:
- Navigate to System > Configuration
- Select the Web section under the General tab
- Scroll down to URL Options
- Set “Add Store Code to URLs” to Yes
Step 2: Configure Base URLs for Each Store
For each store view:
- Go to Stores > Settings > All Stores
- Click Edit next to your desired store
- Expand the Web tab
- In the Base URL fields (Base URL, Secure Base URL), ensure the base domain matches your server configuration
- Save changes and repeat for all stores
Step 3: Update Your .htaccess File
In the public_html folder, open or create a .htaccess file with the following content:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(wow|ahepa|ita|deca)\.example\.com$ [NC]
RewriteRule ^(.*)$ https://%1.example.com/$1 [L,R=301,NE]
# Magento default .htaccess rules go here...
Replace wow, ahepa, ita, and deca with your actual store codes. Also replace example.com with your domain name.
This configuration uses Apache’s mod_rewrite module to rewrite the URL based on the subdomain entered by the user. It captures all requests that match any of the specified subdomains (in this case, wow, ahepa, ita, and deca) and redirects them accordingly while preserving the original path.
Conclusion: Achieving Clean Multi-Store URLs
By following these steps, you should now have clean, distinct URLs for each store within your Magento multi-store setup. The combination of enabling “Add Store Code to Urls” in the admin panel and configuring the .htaccess file ensures that each storefront’s unique code is appended correctly to its respective URLs.
Remember, proper configuration of base URLs for each store view is crucial for this process to work seamlessly across your domain structure. Also, ensure that DNS records are set up correctly on your server to point to the appropriate Magento installation for each subdomain.
This solution provides a robust and reliable method for managing multi-store URLs in Magento, improving SEO, navigation, and overall user experience across your e-commerce platform.
Don’t forget to explore our other articles on Magento development at Laravel Company for more insights into best practices, optimization techniques, and cutting-edge features in the world of Magento.