Magento / Troubleshooting

Magento Multistore Apache: SetEnvIf doesn’t work as expected (prepends REDIRECT_)

by , , revisited on


We have by far the largest RPM repository with dynamic stable NGINX modules and VMODs for Varnish 4.1 and 6.0 LTS. If you want to install nginx, Varnish and lots of useful modules for them, this is your one stop repository to get all performance related software.
You have to maintain an active subscription in order to be able to use the repository!

Intended audience of this solution article

  • You have setup a new store within Magento interface but it doesn’t work.
  • Your Magento is running under Apache web server
  • PHP is configured to run as CGI app

When you setup multiple stores for your Magento shop, these are the typical lines you add to .htaccess file:

SetEnvIf Host .*second-domain.com.* MAGE_RUN_CODE=domain2
SetEnvIf Host .*second-domain.com.* MAGE_RUN_TYPE=website

You may have already pointed the secondary shop domain and added required directives to .htaccess to pass store code to Magento application. But it still won’t work?

Common reason for Apache Magento multistore issues

It seems that the way PHP is configured to run on some hosts, prevents it from reading environment variables set by SetEnv.
However, those variable are still available as REDIRECT_MAGE_RUN_CODE and REDIRECT_MAGE_RUN_TYPE. (prefixed with REDIRECT_)

Solution to multiple stores under Apache

Subsequently, you can create a special file, env.php which checks for those variables and puts them under proper names for Magento:

<?php

if (isset($_SERVER['REDIRECT_MAGE_RUN_CODE'])) {
        $_SERVER['MAGE_RUN_CODE'] = $_SERVER['REDIRECT_MAGE_RUN_CODE'];
}

if (isset($_SERVER['REDIRECT_MAGE_RUN_TYPE'])) {
        $_SERVER['MAGE_RUN_TYPE'] = $_SERVER['REDIRECT_MAGE_RUN_TYPE'];
}

Put the inclusion of env.php atop of core file app/Mage.php which should be sufficient to make it work for all cases:

<?php
include_once dirname(dirname(__FILE__)) . "/env.php";
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:

For a more permanent solution it will require reconfiguring PHP to run as Apache module.
But as long the solution above works well for you, there isn’t much need to change PHP configuration.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.