Magento: Switch between websites using Mage::run($mageRunCode, $mageRunType)

Magento: Switch between websites using Mage::run($mageRunCode, $mageRunType)

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Switch between websites using Mage::run($mageRunCode, $mageRunType)

Magento: Switching Websites Using Mage::run($mageRunCode, $mageRunType)

Switching between websites in Magento is a common requirement for businesses that operate multiple stores with different currencies or product offerings. While the built-in GeoIP functionality works great for sending customers to the appropriate site based on their location, allowing users to manually switch between sites adds another level of flexibility and control.

The Current Approach: A Dropdown Menu

The current implementation uses a dropdown menu in the header to display a list of available websites along with an onchange event that updates the URL when a user selects a different store. Here’s the relevant code:

<div class="website-switcher">
  <label for="select-website"><?php echo $this->__('Select Store:') ?></label>
  <select id="select-website" title="<?php echo $this->__('Select Store') ?>" onchange="location.href=this.value">
    <?php foreach ($websites as $website):
        $_selected = $website->getCode() == Mage::app()->getWebsite()->getCode() ? ' selected="selected"' : '';
        echo '<option value="' . $website->getDefaultStore()->getCurrentUrl() . '"' . $_selected . '>' . $this->htmlEscape($website->getName()) . '</option>';
    endforeach; ?>
  </select>
</div>

However, this approach has a significant limitation: it only changes the website for the current page. As soon as you navigate to another part of the site or click an external link, the user is redirected back to their original website based on the GeoIP settings in index.php.

Understanding Magento’s Website Selection

To understand how to permanently switch websites, we need to look at how Magento sets the active website.

Magento doesn’t use cookies for this purpose. Instead, it uses a more fundamental mechanism called Mage::run($mageRunCode,$mageRunType). This function is responsible for setting up the application’s environment and selecting the appropriate website based on the provided code ($mageRunCode).

By default, Magento sets $mageRunCode to the store code parsed from the request using Mage::getRequest()->getStoreCode(). If this value is empty or invalid, it falls back to the first available website.

Switching Websites Permanently

To make the dropdown menu switch websites permanently, we need to update the $_SERVER['HTTP_HOST'] variable with the new website’s domain name. This tells Magento to select that specific site when processing subsequent requests.

Here’s how you can do this using a custom controller action:

public function switchWebsiteAction()
{
    $newWebsiteCode = $this->getRequest()->getParam('website');
    $website = Mage::app()->getWebsite($newWebsiteCode);

    // Update the domain name for the current request
    $_SERVER['HTTP_HOST'] = $website->getConfig(Mage_Core_Model_Store::XML_PATH_UNSECURE_BASE_URL);
    $_SERVER['HTTP_HOST'] = str_replace('www.', '', $_SERVER['HTTP_HOST']);

    // Redirect to the home page of the new website
    $url = Mage::getBaseUrl();
    header("Location: {$url}");

    exit;
}

In this code, we first retrieve the selected website based on the website parameter passed to the action. We then update the $_SERVER['HTTP_HOST'] variable with the unsecured base URL of the new website.

After updating the domain name, we redirect the user to the home page of the new site using a relative URL generated by Mage::getBaseUrl(). This ensures that the new website is selected for all subsequent requests within the same session.

Best Practices and Considerations

  1. Clear Cart: When switching between websites with different currencies or product offerings, it’s essential to clear the customer’s cart during the transition. You can achieve this by adding Mage::getSingleton('core/session')->unsCart() in your action before redirecting.

  2. URL Structure: Keep in mind that updating the $_SERVER['HTTP_HOST'] variable changes the domain name in the URLs displayed on the site. If you don’t want to enable “Add Store Code To Urls,” ensure that your hosting setup is configured correctly to support multiple domains with distinct content for each.

  3. Caching: Magento’s caching mechanisms might interfere with the website switching process. Ensure that you have proper cache management in place, such as disabling or clearing caches during transition requests.

Conclusion

Switching websites permanently in Magento requires understanding the underlying mechanism used by Mage::run() to select the active site based on the domain name provided in $_SERVER['HTTP_HOST'].

By updating this variable programmatically using a custom controller action and redirecting with a relative URL, you can create a seamless experience for users to switch between different websites within your Magento installation.

Remember that this approach has some considerations regarding cart clearing, URL structure, and cache management. Proper planning and implementation will ensure a smooth transition for both customers and the system’s performance.

For more information on advanced Magento customizations and best practices, visit Laravel Company to explore our comprehensive range of services tailored for e-commerce success.

Catalog Customers CLI

Our network