htaccess redirect subdomain to domain magento store

htaccess redirect subdomain to domain magento store

August 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
htaccess redirect subdomain to domain magento store

Fixing Magento Store URL Issues on Subdomains

When working with Magento stores hosted on subdomains, you might encounter issues where the main store’s base URL is used instead of the specific subdomain’s URL for certain operations like AJAX Quick View and Add to Cart. This problem often arises due to the way themes use Mage::getUrl() to generate URLs.

Understanding the Base URL Issue

The core issue lies in how Magento handles the base URL for each store view. By default, when you create a new subdomain as a separate store view, it inherits the base URL from the main domain (site.com). This means that even though your theme is set to use the specific subdomain’s URL, the actual base URL remains the same as the primary store.

Why htaccess Redirection Fails

The suggested htaccess rule tries to redirect certain URLs based on specific patterns. However, this approach fails because it only matches against the REQUEST_URI, which contains the path information after the domain name. For subdomains like sub1.site.com, the REQUEST_URI will always start with /catalog/ajax_product/..., making the rewrite rule unable to distinguish between the main domain and the subdomain.

The Correct Approach: Overriding Mage::getUrl()

To resolve this issue, you need to override how Magento generates URLs for your specific theme. Instead of using Mage::getUrl(), which pulls the base URL from the store configuration, you can create a custom function that constructs the URL manually.

  1. Create a new helper file in your theme (e.g., Helper/Data.php).

  2. In this helper file, define a new method, say getCustomUrl($route, $params = null), which will construct the URL based on the desired domain:

public function getCustomUrl($route, $params = null)
{
    if ($route === 'catalog/ajax_product/view') {
        return 'http://' . $_SERVER['HTTP_HOST'] . '/catalog/ajax_product/view';
    }
    // Handle other routes similarly...
}
  1. In your theme’s layout XML files or template files, replace the usage of Mage::getUrl() with a call to this new helper method:
<reference name="head">
    <block type="core/text" name="custom_js_include">
        <action method="setText">
            <text><![CDATA[
                $.post('<?php echo $this->helper('yourtheme')->getCustomUrl('catalog/ajax_product/view') ?>', function(data) {
                    // Your JavaScript code here
                });
            ]]>
            </text>
        </action>
    </block>
</reference>

Conclusion

Fixing Magento store URL issues on subdomains requires a deeper understanding of how the platform handles base URLs and a strategic approach to overriding default behavior. The htaccess method suggested initially fails because it cannot differentiate between requests from the main domain and the subdomain.

By creating a custom helper function that constructs the URL manually based on the desired domain, you can ensure your theme generates the correct URLs for operations like AJAX Quick View and Add to Cart. This approach maintains the integrity of your store’s configuration while allowing your theme to work correctly across all domains.

Remember, it’s crucial to test this solution thoroughly in a development environment before applying it to your live Magento installation. For more detailed guides on Magento theme customization and URL management, consider visiting Laravel Company.

Checkout Catalog Themes Configuration

Our network