Setting environment variables for your Magento store through .htaccess

Once in a while i get the question, Martijn how can we select a storeview without editting the index.php file, as Magento is prepared for this?!

Well, the Magento index.php file is indeed prepared for this. The full Magento index.php for releases and later is as below.

* Magento
* 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:
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to so we can send you a copy immediately.
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to for more information.
* @category   Mage
* @package    Mage
* @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (
* @license  Open Software License (OSL 3.0)

if (version_compare(phpversion(), '5.2.0', '<')===true) {
    echo  '<div style="font:12px/1.35em arial, helvetica, sans-serif;"><div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;"><h3 style="margin:0; font-size:1.7em; font-weight:normal; text-transform:none; text-align:left; color:#2f2f2f;">Whoops, it looks like you have an invalid PHP version.</h3></div><p>Magento supports PHP 5.2.0 or newer. <a href="" target="">Find out</a> how to install</a> Magento using PHP-CGI as a work-around.</p></div>';

* Error reporting
error_reporting(E_ALL | E_STRICT);

* Compilation includes configuration file
$compilerConfig = 'includes/config.php';
if (file_exists($compilerConfig)) {
    include $compilerConfig;

$mageFilename = 'app/Mage.php';
$maintenanceFile = 'maintenance.flag';

if (!file_exists($mageFilename)) {
    if (is_dir('downloader')) {
        header("Location: downloader");
    } else {
        echo $mageFilename." was not found";

if (file_exists($maintenanceFile)) {
    include_once dirname(__FILE__) . '/errors/503.php';

require_once $mageFilename;



#ini_set('display_errors', 1);


/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

Mage::run($mageRunCode, $mageRunType);

On lines 66 you will find a conditional test that would allow you to turn on the Magento Developer-mode. On lines 75 and 78 you will see how the $mageRunCode and $mageRunType is populated by server values.

All three values can be set from the .htaccess files or vhost configurations of your server with the code below.

### Setting environment variables for your Magento store.
SetEnvIf Host "" MAGE_RUN_CODE=storeview_code

### Next line is important for multi language stores on the same domain.
### When running multiple languages on one domain, the storeview code is
### stored in the frontend cookie. Without this line, your store would
### default back to the storeview set above
SetEnvIfNoCase ^Cookie$ "frontend=(.+)" MAGE_RUN_CODE=

With the code at line 2, you can turn on the developer mode for only the specified ip address. The code at line 3 allows you to set a specific storeview-code to be used while executing the Magento web-app.

However, setting always a specific storeview-code is a bad idea. In multilingual stores you could make it default back to the default language all the time. Therefor i recommend not to set a storeview if a cookie is set with a frontend-id.

I hope this is useful for all the Magento Developers out there. If you like my articles, please spread the word using the Tweet button or the Facebook-‘Recommend’-button.

By the way, don’t forget that Magento redirects by default to your base url. So be aware that you need to set this storeview url for a second store in the General Web section of the System config