Multiple websites on Magento 1.4 single installation

Last modified by Bart Simpson 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';
  3.  
  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} ^(.*)site.com
RewriteRule ^ - [E=MAGE_RUN_CODE:example]

or

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 'german.example.com':
  4.   $store = 'german';
  5.   break;
  6.  
  7. default:
  8.   $store = 'base';
  9. }
  10.  
  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.

Multiple Website Setup for US/EU/UK Stores/Pricing

If you want to set your own prices for $/€/£ and not have potential customers from the US get to see your UK pricing then you can install Apache mod_geoip and use the country codes in your index.php to direct your visitor to the correct website.

Note that using websites rather than stores may be necessary if you are not using automatic currency conversion, i.e. you set the $, € and £ prices to be sensible figures, e.g. ending in a .99 and not whatever gets converted that day from your base currency.

You may not be able to run mod_geoip if you are using cPanel or shared hosting. Refer to your distro package manager for installing mod_geoip and download the dat files from maxmind (if necessary).

The normal instructions for doing anything clever with mod_geoip involve htaccess and apache config files, here, once mod_geoip is installed and enabled, there is no need to do anything with the apache config files or htaccess. Only index.php needs modifying.

Change this section:

$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

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

...to:

switch ($_SERVER['GEOIP_COUNTRY_CODE'])
{ case "CA":
  case "MX":
  case "US":
    $_SERVER["MAGE_RUN_CODE"] = "usa";
    $_SERVER["MAGE_RUN_TYPE"] = "website";
    Mage::run('usa', 'website');
    break;
  case "AT":
  case "BE":
  case "CY":
  case "DE":
  case "ES":
  case "FI":
  case "FR":
  case "GR":
  case "IE":
  case "IT":
  case "LU":
  case "MT":
  case "NL":
  case "PL":
  case "SI":
  case "SK":
    $_SERVER["MAGE_RUN_CODE"] = "europe";
    $_SERVER["MAGE_RUN_TYPE"] = "website";
    Mage::run('europe', 'website');
    break;
  default:
    $_SERVER["MAGE_RUN_CODE"] = "base";
    $_SERVER["MAGE_RUN_TYPE"] = "website";
    Mage::run('base', 'website');
}

You should not have to worry about refreshing caches etc. to get this to work, however, you may want to do some online testing using proxy websites in different countries.

Note that in this example the three website codes are ‘base’, ‘europe’ and ‘usa’. Anything not picked up as North American or Euro-zone gets treated as ‘base’, in this case the UK. (This example is for a Britisher store.)

If using this technique then you can change the default option in the php case statement to test the other websites. Note that in the backend you need to set the websites URL‘s to all be the same, i.e. with no http://www.example.com/eu /us /uk variants, just http://www.example.com will do. Everything also goes through to the same document root.

If using this technique then you do not have to use different flags at the top of the page to ‘select your own currency’, however, there is no reason why you cannot enable currency translation for your base store and show the currency selector if the GEOIP value is not that of your base country. In that way you can keep a clean and tidy page design.




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs