Introduction

This document describes how to run multiple Magento domains with different document roots, e.g. in separate accounts on a VPS. In this setup, each domain name points to a separate account on the server, with a separate document root:

If you only intend to run Magento, there may not be any immediate advantages with using separate accounts, except perhaps that it constitutes a slightly cleaner approach that may be easier to manage.

When Magento is integrated with other server applications, however, things may get quite a lot more 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.

In this document, we will refer to the account where Magento is installed as “magentohome”, and to the accounts where Magento is running stores as “store1”, “store2” etc.

This example assumes that these accounts are used with Magento 1.1.8, PHP5 and the Apache web server in a Unix environment.

In the example setup, Magento is installed in /home/magentohome/public_html/magento/ and one of the store views is running from /home/store1/public_html/en/. In your setup, the format of these paths may be different; please consult the documentation for your web hosting environment.

Magento Installation

Install Magento as usual in the “magentohome” account.

Creating the First Store

The files needed to run store1 are all stored in the /home/store1/public_html/ directory.

In the example, we use a separate store view for each language we support in the store. To simplify bookmarking of pages in the store, we use a separate directory for each language/store view. Consider what folder structure best serves the store views you will be using.

The first language we support in this example is English. The files for the English store view are stored in/home/store1/public_html/en/.

The files we use for each store view are:

index.php

.htaccess

The files we use in the root folder for each store are:

index.php

.htaccess

http404.php

sitemap files

robots.txt

The Store View index.php File

In the example, each store view has its own directory, e.g. “/en”. Each of these directories needs the basic Magento index.php file to load /magento/app/Mage.php and issue the Mage::run call.

We need to make two modifications to this file: we need to provide the full path to the Magento directory, and we need to call the appropriate store view.

The full path to the Magento directory is /home/magentohome/public_html/magento/, so we modify the line where $mageFilename is set to

$mageFilename = '/home/magentohome/public_html/magento/app/Mage.php';

The store view has been defined in Admin > System > Manage Stores as “store1en”, so we modify the Mage::run call to

Mage::run('store1en');

The Store View .htaccess File

Each store view also needs a copy of the basic Magento .htaccess file in its directory, tailored to your requirements. As an example, if you run PHP5 in CGI mode, you may have uncommented the first few lines in the default Magento .htaccess file.

In this example, we only need modify the RewriteBase statement (near line 120 in Magento 1.1.8) in the default .htaccess file.

RewriteBase /en/

This tells the Apache server that everything that comes after the folder name in each URI is really parameters to the Magento index.php file.

Running Magento without URL rewrites

Using URL rewrites can create a lot of problems later, when you want to run other content delivery systems (blogs, forums etc) in the same language folder. Since all requests for content in the “en” folder will be rewritten to the Magento index.php file, it will be difficult to run anything else but Magento.

A more flexible approach is to rename the index.php file to something more search-engine-optimized, e.g. “buy”. Your baseURL will then become

http://store1.com/en/buy/

which doesn’t look that bad. To tell the Apache server that “buy” should be run through the PHP interpreter, you need to add code similar to the below example to your .htaccess file. The way to do this varies slightly between Apache versions, however.

<Files buy>
ForceType application/x-httpd-php
</Files>

The Root Folder index.php File

The main purpose of the index.php file in the root folder of the example setup is to activate the default store view, in our case by doing a redirect to the “en” directory. We accomplish this with the below index.php file:

The Root Folder http404.php File

Magento has it’s own handler for http 404 “Not Found” errors, and in the Demo Store display the “no-route” page in the Magento CMS for 404 errors. This feature only works when the URI is correct up to and including a Magento folder name, so that the Magento index.php file is run. In our example, any incorrect URI beginning with “http://store1.com/en/” will take the user to the “no-route” page.

If the URI begins with “http://store1.com/de/“, however, Apache will signal the http 404 error to the default 404 error handler designated by the .htaccess file in the root folder. In the example, the http404.php file will be run. Our objective is to show the Magento “no-route” page for all “Not Found” errors, so we create a http404.php file that looks like this:

The Root Folder Sitemap Files

Magento can generate a sitemap file for a single store view, but unfortunately not yet for a whole store or website. This means that you will have to submit each sitemap file separately to the search engines, preferably via the robots.txt file as shown below.

Most (if not all) search engines require that a sitemap is located in the domain it describes. Magento can only generate sitemap files in the Magento directory or a subfolder of it, e.g. /home/magentohome/public_html/magento/. When Magento stores are run with separate document roots, you must make the sitemap appear to be present in the document root of each store, e.g. /home/store1/public_html/.

In the example, the XML format sitemap file generated by Magento is located at/home/magentohome/public_html/magento/store1en_sitemap.xml. We now create a file called/home/store1/public_html/store1en_sitemap.php:

The Root Folder robots.txt File

The robots.txt file is mainly used to exclude one or more directories or files from indexing by the search engines. The robots.txt file format is described at http://www.robotstxt.org/. Note that many search engines allow the use of wildcards, support the setting of crawl rates, allow sitemap links and other more recent additions to the robots.txt standard, while others only support the basic Disallow directive.

Magento often provides many ways to reach a particular page, e.g. by the full /catalog/product/view/id/ link to a product page in the catalog, or using the URL rewrite for the page. The main reason for excluding directories from indexing is to have the search engine use only one distinct, preferred address for each page. A small robots.txt file for a Magento site can look like this:

With Magento, there are many more directories that you may want to exclude from indexing. If you search the Magento forums, you will see recommendations to exclude one or more of the following directories for a Magento site:

/index.php/

/404/

/admin/

/api/

/app/

/catalog/category/view/

/catalog/product/view/

/catalog/product_compare/

/catalogsearch/

/cgi-bin/

/checkout/

/contacts/

/customer/

/downloader/

/install/

/images/

/js/

/lib/

/magento/

/media/

/newsletter/

/pkginfo/

/private/

/poll/

/report/

/review/

/sendfriend/

/skin/

/tag/

/var/

/wishlist/

For the more advanced search engines, you may specify a delay between each access to your site, e.g. 5 seconds. It is also possible to avoid indexing any URLs with parameters, e.g. http://store1.com/en/maincategory?mode=grid.

User-agent: msnbot
Crawl-delay: 5
Disallow: /*?

At the end of the robots.txt file, you should insert a link to each of the sitemap files for the site.

Modifications to the Magento Configuration

You configure each website, store and store view in Admin > System > Configuration. You must assign the base URLs for each store view on the Web tab, and set your design options on the Design tab.

Before you do this, you may need to add your new web site with its stores and store views to your installation using Admin > System > Manage Stores.

Three Magento directories must be accessible from all domains running Magento:

The skin directory that contain the CSS, images and JavaScript for your themes

The media directory, where all the product images are saved

The js directory where most of Magento’s JavaScript code resides

In Admin > System > Configuration > Web you specify the location of these directories for all your stores. You basically have three options: point to the main magento directories for each website, copy the three directories to each website, or use symbolic links.

Use the Magento Directories For All Websites

The Magento installation includes all the files you need. In theory, you can simply link all your websites to the main Magento folder with the following settings in Admin > System > Configuration > Web > Unsecure:

The main issue with this setup is if the secure pages on store1.com really can include secure content from magentohome.com, which has a different SSL certificate. If you know the answer, please update this section!

Copy Important Magento Directories To All Websites

To get around the possible SSL problem discussed above, you can just copy the three folders from magentohome.com to store1.com, store2.com etc, e.g. using SSH with root access:

The main disadvantage with this solution is that you must propagate all updates to your Magento installation - new product images, new scripts or new themes - to all your websites before you can use them. If you work a lot with your site’s contents or design may need to run the above copy job quite often.

Using Symbolic Links For the Important Magento Directories

To avoid the inconvenience of having to copy all the files to all your Magento-driven websites, you may use symbolic links in the server’s file system. Symbolic links can be used to make directories in one account accessible also in the directory structure of another account.

To access these three directories using symbolic links, open an SSH connection to your server and login with root access. Then create symbolic links to the three Magento directories using this sequence of commands:

You have now created “aliases” for the three Magento folders for store1. The symbolic links allow you to reference all the related files in the store1.com domain even though they are only stored on magentohome.com.

This allows you to use the following settings in Admin > System > Configuration > Web > Unsecure: