How to Setup Magento with Multiple Stores and Domains


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.

Prerequisites

  • Magento must be installed
  • Access to add new vhosts (virtual hosts). Most control panels (like cPanel) supports this.

URL structure

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:

  • Domain (e.g. www.store1.com and www.store2.com)
  • Subdomain (e.g. store1.mystore.com and store2.mystore.com)
  • Folder (e.g. mystore.com/store1/ and mystore.com/store2/)

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.

Server configuration

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.

Parked Domains Parked Domain in cPanel

Add new Magento store

The next step is to create the actual stores in Magento. Lets say we want to add a new store mysecondstore.com.

Create root category

If you want your websites to share the same catalog and categories you can skip this step.

  1. Log into your Magento Admin Panel
  2. Click on Catalog Manage Categories
    magento-categories
  3. Click on Add Root Category
    magento-root-catalog
  4. Enter a name for the new category and make sure Is Active is set to True
  5. Click on the Display Settings tab and set Is Anchor to True. This will show products listed in sub categories, and also enable product drill-down functionality (filtering) for the category.
    magento-category-anchor
  6. Click Save Category

Store configuration

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.

  1. Go to System > Manage Stores
  2. Click Create Website and enter the following information:
    (Skip this step if you don't want separate Websites)
    • Name - enter a name for the new website
    • Code - enter a unique identifier for this website
      Make a note of this code as you will need it later!

    magento-create-website

  3. Click Create Store and enter the following:
    • Website select the website you just created from the dropdown;
    • Name - enter a name for the store;
    • Root Category - select the category you created in the previous step.

    magento-add-store

  4. Click Create Store View and the information as follows
    • Store - select the store you created in the previous step
    • Name - enter a name for the store view
    • Code - enter a unique identifier for this store view
      Make a note of this code as you will need it later!

    This is where you will add additional store views if you plan on setting up a site for each language you support for example.
    magento-add-storeview

  5. Now, go to System > Configuration > General
  6. Make sure Default Config is selected as the Current Configuration Scope
    magento-config-scope
  7. On the Web tab, set Auto-redirect to Base URL to No and click Save Config
    magento-web-settings
  8. Now, change the Configuration Scope dropdown to you newly created website
  9. Under the Web section, we now need to change the Secure Base URL and Unsecure Base URL settings. Uncheck the Use Default [STORE VIEW], and replace the URL's with your corresponding domain name. Remember to include the trailing /.
    magento-site-urls
  10. Click Save Configuration

This completes the configuration of the new store. Repeat these steps for each additional store you want to add.

Domain mapping

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.

  1. On the server, open the .htaccess file, located in the root directory of your Magento installation, in your favorite text editor. You can also use the File Manager in your control panel.
  2. Add the following lines (replace with actual domain names):
    
    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]
    
    
    • MAGE_RUN_CODE - this is the unique code chosen when you created the Magento Website / Store View
    • MAGE_RUN_TYPE - depending on whether you want to load a specific Website or Store View; set it to website or store, respectively.

    Add an entry for each additional domain you have set up.

  3. Save the file

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.

Note about Magento versions prior to 1.4

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.

  1. Open index.php in any text editor
  2. Look for the following line near the bottom of the file:
    Mage::run($mageRunCode, $mageRunType);
  3. Add the following lines right above this line:
    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 domaincode and type according to your particular setup. Add additional case statements if you have more stores.

  4. Save the file

You should now be able to see each respective store by browsing the URL.

Good luck!


Was this answer helpful?
       
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-41868009-1', 'auto'); ga('require', 'displayfeatures'); ga('send', 'pageview');