One of the useful features in Magento is the ability to create multiple stores/websites that share the same Magento installation. This allows multiple store fronts to share a common code base and backend, making administration a lot easier. Stores can share customer base, product catalog and settings based on how you choose to configure your sites.
This tutorial will go through the steps of setting up multiple stores in Magento, and how to configure a domain for each store.
Before you start adding new stores to Magento, you should have a clear understanding of how each store will be addressed, i.e. how the URL will look like. You basically have three approaches:
How you choose to structure your stores is a matter of personal preference, and the steps for setting this up will be more or less the same. Generally, subdomains are often used to create localized or area-specific versions of the store (e.g. es.mybestshop.com and en.mybestshop.com). Since most users will choose either the domain or subdomain method, this is the method I will describe in this tutorial.
Before we create the new Magento stores, we need to configure each domain/subdomain to resolve to the same Magento installation. That is, the document root of each virtual host must be set to the file path where Magento is installed. How this is done depends on the web server and control panel. For this example, we are assuming the server is running the Apache Web Server and the cPanel control panel. The method will be relatively similar for other control panels.
Assuming your main domain is already set up in cPanel, we just need to add the additional domains/subdomains. We will use the Parked Domains feature in cPanel to add the additional domains. Parked domains are similar to aliases, so they will automatically resolve to the same file directory. If you chose the subdomain method, just create the subdomains and point them to the directory where Magento is installed.
The next step is to create the actual stores in Magento. Lets say we want to add a new store mysecondstore.com.
If you want your websites to share the same catalog and categories you can skip this step.
Before we start configuring our stores, lets pause for a minute and explain the concepts of Websites, Stores, and Store Views in Magento. Websites are the top-most entity in Magento. If you want completely separate sites that do not share cart, shipping methods, etc., you should create separate Websites. Each Website has at least one Store, and each Store has at least one Store View. Multiple Stores can share cart, user sessions, payment gateways, etc., but have their own catalog structure. Finally, a Store is a collection of Store Views. Store Views change the way pages are presented, normally used to offer a site in different layouts or languages. These concepts can be a bit confusing at first and I recommend this webinar which explains the Magento multi-store retailing in detail.
This is where you will add additional store views if you plan on setting up a site for each language you support for example.
This completes the configuration of the new store. Repeat these steps for each additional store you want to add.
So far we have added the additional domains to the server and configured the new store in Magento. Now we just need to glue it together by telling Magento which store to load based on the domain name the user is on.
SetEnvIf Host www\.domain1\.com MAGE_RUN_CODE=domain1_com
SetEnvIf Host www\.domain1\.com MAGE_RUN_TYPE=website
SetEnvIf Host ^domain1\.com MAGE_RUN_CODE=domain1_com
SetEnvIf Host ^domain1\.com MAGE_RUN_TYPE=website
SetEnvIf Host www\.domain2\.com MAGE_RUN_CODE=domain2_com
SetEnvIf Host www\.domain2\.com MAGE_RUN_TYPE=website
SetEnvIf Host ^domain2\.com MAGE_RUN_CODE=domain2_com
SetEnvIf Host ^domain2\.com MAGE_RUN_TYPE=website
...
Note: the SetEnvIf directive is not supported by all web servers (e.g. LiteSpeed). In that case, the store code can be set in this way:
RewriteCond %{HTTP_HOST} www\.domain1\.com [NC]
RewriteRule .* - [E=MAGE_RUN_CODE:domain1_com]
RewriteCond %{HTTP_HOST} www\.domain1\.com [NC]
RewriteRule .* - [E=MAGE_RUN_TYPE:website]
RewriteCond %{HTTP_HOST} www\.domain2\.com [NC]
RewriteRule .* - [E=MAGE_RUN_CODE:domain2_com]
RewriteCond %{HTTP_HOST} www\.domain2\.com [NC]
RewriteRule .* - [E=MAGE_RUN_TYPE:website]
Add an entry for each additional domain you have set up.
Now navigate to you new site and verify that the correct Magento store is loaded. If not, make sure that the the MAGE_RUN_CODE match the one created earlier and that the domain resolves to the correct folder/path.
The ability to set store code using environmental variables were introduced in Magento 1.4. If you are running an older version of Magento, or your webserver does not support Apache environmental variables, you must set the store code using a different method.
Mage::run($mageRunCode, $mageRunType);
switch($_SERVER['HTTP_HOST']) {
case 'domain1.com':
case 'www.domain1.com':
$mageRunCode = 'domain1_com';
$mageRunType = 'website';
break;
case 'domain2.com':
case 'www.domain2.com':
$mageRunCode = 'domain2_com';
$mageRunType = 'website';
break;
}
Replace the domain, code and type according to your particular setup. Add additional case statements if you have more stores.
You should now be able to see each respective store by browsing the URL.
Good luck!