Multiple websites on Magento 1.4 single installation

Last modified by goolic on Fri, December 3, 2010 17:09
Source|Old Revisions  

This is an old revision of the document!

Multiple websites on Magento 1.4 single installation

From 1.4, Magento supports multiple websites on one single installation.

Solutions used in previous versions required developer to modify index.php file to handle different domains pointing at different stores. Since 1.4 index.php contains the following code:

  1. $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
  2. $mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
  4. Mage::run($mageRunCode, $mageRunType);

The code checks two environmental variables and use them to start Magento with the right website/store which can be defined in Manage Stores section in Admin. You can set these environmental variables in your virtual host definition or in the .htaccess file in your installation root.

VirtualHost solution

Add following lines within your VirtualHost definition:

  1. SetEnv MAGE_RUN_CODE "base" # put here your website or store code
  2. SetEnv MAGE_RUN_TYPE "website" # put here 'website' or 'store'

.htaccess solution

If you have no access to virtual host definitions, you can add the following lines to your .htaccess file:

RewriteCond %{HTTP_HOST} ^(.*)
RewriteRule ^ - [E=MAGE_RUN_CODE:example]


SetEnvIf Host .*site.* MAGE_RUN_CODE=example
SetEnvIf Host .*site.* MAGE_RUN_TYPE=website # 'website' or 'store'

where example is the code defined for the website in the Manage Stores section. Repeat for each domain/website combination.

Multiple website setup on versions before 1.4

There are many ways multiple stores can be implemented. it could be:

  • subdomains in different document roots,
  • subdomains in same document root,
  • subfolders,
  • folder aliases,
  • etc.

To keep things flexible we designed it that the user can decide on the logic of which store will be launched from a specific location.

All the logic should be done from the index.php file.

As you can see in default index.php we have:

  1. Mage::run('base');

That means that the ‘base’ store will be invoked.

In the demo download package we have multiple stores defined by the subfolder they’re in.

For example the German store is in /de/index.php:

  1. Mage::run('german');

If you have the same index.php serving as an entry point for a few stores, the store code will have to be calculated from the environment, for example, host name:

  1. $host = explode(':', $_SERVER['HTTP_HOST']);
  2. switch ($host[0]) {
  3. case '':
  4.   $store = 'german';
  5.   break;
  7. default:
  8.   $store = 'base';
  9. }
  11. Mage::run($store);

Please note that .htaccess should be copied to every folder index.php is in.

Important: if you want your stores/websites to share the same cookies, you should fill in the “Session Cookie management” section of the Web Configuration with /. Otherwise each store will have its own cookies, which means, for example, that if you are trying to use the cookie “store” (that contains the current storecode) for localization purposes, you may not have access to the expected cookie and the retrieved storecode may appear random.

Multiple Website Setup with Different Document Roots

When Magento is integrated with other server applications, things may get quite complicated. If you intend to provide a blog, a discussion forum and/or a chat server together with your Magento store, just parking all the domain names on top of a shared account may become impossible or at least increasingly messy to manage. Separating each Magento domain in a separate account allows you to integrate each store separately with the other web applications it needs in a much simpler fashion.

If this sounds interesting, read more about multiple website setup with different document roots.