Multiple stores in one Magento install

ukDeze post is ook beschikbaar in het Nederlands.

Lately, we've been receiving a lot of questions on whether or not it would be possible to set up more than one webshop in a single Magento installation. Magento does indeed have this option, but the exact implementation may differ from case to case and from shop to shop. Another question we get a lot is "How do I set up an extra domain in Magento?". To take away some of the mysteries and questions, we'll give a brief explanation about how we have set up multiple Magento stores in one installation.

Extra domainnames in Magento

Every shop requires its own domain name. Say we sell exotic animals like camels (kamelen in Dutch) and we would like to open an online shop. First of all we would have registered the domain name www.kamelen-online.nl with Byte's Magento hosting services. We chose a package with some extra diskspace so the extra shops and products should fit nicely.
Because we want to sell these animals both in the Netherlands and abroad, we registered a second domain: www.camels-online.com. We also have dromedaries for sale, so we registered a third domain: www.dromedaris-online.nl.
At Byte, we get a Presence hosting package with these names, as these allow us to setup a Server Alias to connect these domains to the existing hosting space (www.kamelen-online.nl in our example).

Websites, stores, views...Stores en Websites

Before we continue, it is important to understand the difference between some common Magento terms: Websites, Stores and Store Views.

  • The difference between a Website and a Store, is that a websites only contains customer data but no products and categories.
  • Stores are the real shops. They contain all the products that the shop offers. Stores may have one or multiple Store Views.
  • Store Views are the different ways the Stores are being presented. For example, you could setup a different Store View per language.

Every Magento site will always have at least one Website, one Store and one Store View, all created in the Magento Admin interface (under System --> Manage Stores). In our case (www.kamelen-online.nl) we set up the following:
We created one Website, called 'Kamelen site', with one Store, called 'Kamelen shop'. We gave this Store two seperate Store Views: one in Dutch and one in English.
We also created a second Website called 'Dromedaris site', with one Store and only one Store View.
When creating Store Views, Magento asks for a code to use to reference these Store Views. We chose for 'kamelen_nl', 'kamelen_en' and 'dromedaris'. These codes are important as we will need them later on to connect them to the domain names.

Magento Multistore Settings

Magento Multistore Setting


Connecting Domain names to Store Views

One of the first things Magento does when it loads is start up the Store View. Normally it starts the default Store View, but obviously you can change this, i.e. based on the domain the user is visiting to get to the website. The only thing we have to do is determine what Store view is being visited and start Magento with the code of that store front.
It's not yet possible to connect domainnames with Store Views through the Magento backend, but luckily, this is a very simple procedure as you only need to change one file. Which file that is, depends on the version of Magento you use.

Connecting domains in Magento 1.3

In Magento 1.3 you can connect the domain name and Store View through the index.php file. This file loads all Magento files and subsequently starts the Website. Look for the following line in PHP:

Mage::run();

Replace this line with the following:

switch ($_SERVER['HTTP_HOST']) {

	//Customer visits the dutch camel shop
	case 'www.kamelen-online.nl':
	case 'kamelen-online.nl':
		Mage::run('kamelen_nl', 'store');
	break;

	//Customer visits the international camel shop
	case 'www.camels-online.com':
	case 'camels-online.com':
		Mage::run('kamelen_en', 'store');
	break;

	//Customer visits the dromedaries shop
	case 'www.dromedaris-online.nl':
	case 'dromedaris-online.nl':
		Mage::run('dromedaris', 'store');
	break;

	default:
		Mage::run();
	break;
}

Connecting domains in Magento 1.4

One of the differences between Magento versions 1.3 and 1.4 is that the code for detecting specific store fronts was removed from the default Magento files. This prevents all Store Views ending up at the same Website, or worse, a total breakdown of Magento, after upgrading the webshop.

Extra patch for Byte Hosting

At Byte Internet we use the 'suExec' principle for our webhosting, however the usual way to choose a Store View in Magento isn't 100% compatible with this extra secured Magento setup. 'suExec' keeps your site extra secure, as it prevents unauthorized users from accessing your files. It also cleans up the environment variables to prevent your site from behaving differently due to them. Unfortunately Magento uses these environment variables to determine which Store should be loaded.
In order to make use of the variables stored in the environment you need to make a small adjustment to the index.php file. Around line 75 you'll find the following line:

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

Replace this with:

$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] :
(isset($_SERVER['REDIRECT_MAGE_RUN_CODE']) ? $_SERVER['REDIRECT_MAGE_RUN_CODE'] : '');

Once you've made this change, Magento will know to also check the cleaned up environment variables to determine what Store View to load.

In Magento 1.4, the logic to determine the right shop is in the .htaccess file. This file sets up the environment variables that the index.php file will then consult to know what shop to load.

In the .htaccess file we added the following lines:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(.*)kamelen-online.nl
RewriteRule ^ - [E=MAGE_RUN_CODE:kamelen_nl]

RewriteCond %{HTTP_HOST} ^(.*)camels-online.com
RewriteRule ^ - [E=MAGE_RUN_CODE:camels_en]

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

In this example there are 3 different Stores. All domains ending with 'kamelen-online.nl' will lead to the 'kamelen_nl' Store. 'camels-online.com' will lead to the English Store and 'dromedaries-online.com' is setup as well.

Configure different shops

Magento Config Scope

Magento Config Scop

The only thing left to do is setup the different stores in Magento. By default, the settings for all Stores are automatically the same as the Website settings and those in turn are linked to the global settings. You can change settings on al levels, and have them propagate downwards. You can change between levels by selecting the appropriate view, shop or global level in the drop down menu found in the Magento configuration editor in the top left.

The most important thing to change, is the base URL. You'll find the setting in the Magento backend under System\Configuration\Web\Unsecure and \Secure. We've put the URL's that Magento uses as the standard URL at all levels.

In our example under "Default Config" we've used "http://www.kamelen-online.nl/" as base URL as this is the main domain. The English version under "Kamelen Store" has http://www.camels-online.com/" as it's base URL ", and the 'Dromedarissen'-version obviously has "http://www.dromedaris-online.nl/" . If these shops have a HTTPS / Secure version as well, then you should also fill in the https-base-URL's under "Secure".

 

Magento baseURL settings

Magento baseURL settings

 

Obviously, creating different stores is just the beginning. There are still many options left in the Magento Admin Panel, but for now, we'll leave it at this. If you have any tips or tricks for multiple Magento stores, feel free to let us know!

If you liked this post, say thanks by sharing it:
  • Erik Pluijmen

    We have fixed and added to the blogpost by explaining a little more about base url’s. We hope that users who experienced 404 errors before don’t run into them anymore

  • http://www.itbiz.com.br Lucas

    Do you know, please, how to setup multiple stores with multiple backends in one single magento install?

  • Cipriano Groenendal

    Hi Lucas,

    While I think it is possible to do this, I wouldn’t know of a simple way to get this up without applying a lot of hosting environment specific magic. You might be able to have a different local.xml selected dependent upon which Hostname is requested using a .htaccess or additions in the index.php, but i wouldn’t know a solution off’ the top of my head.

    Personally I would advise against it, since this could cause quite a few problems, such as:

    Different shops wanting to use the same theme but with slight changes for one of them.
    Different shops wanting to use different modules, but all of them being installed for all shops.
    Different shops needing to use different Magento versions instead of the same
    Different shops saving media with conflicting filenames

    if you still wish to use multiple stores in one single install, I wish you good luck.

  • http://about.me/hans2103 hans2103

    successfully tested on Magento 1.7
    The tutorial how to create a multistore Magento installation for Magento 1.4 also works for Magento 1.7

    change index.php because of suExec and change .htaccess with the redirects.