Multiple Website Setup with Different Document Roots

Last modified by Magnus Wester on Wed, June 23, 2010 15:14
Source|Old Revisions  

This is an old revision of the document!


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:

  /home/magentohome/public_html/magento/

  /home/store1/public_html/en/
  /home/store1/public_html/de/
  /home/store1/public_html/fr/
  /home/store1/public_html/es/
  /home/store1/public_html/it/

  /home/store2/public_html/en/
  /home/store1/public_html/sv/


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.6, 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.

We’ll use http://magentohome.com/ for http links to the account where Magento installed, and http://store1.com/ when we link to the store1 store.

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

  1. $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

  1. 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.6) 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.

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:

  1. // /store1.com/public_html/index.php
  2.  
  3. // Used in multi-language environments to pass control to
  4. // the index page in the default language folder
  5.  
  6. // Ensure the cached copy is ignored
  7. // Expiration date is in the past
  8. header("Expires: Fri, 14 Oct 1955 05:00:00 GMT");
  9.  
  10. // The page has always been modified since you saw it last
  11. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  12.  
  13. // HTTP/1.1 (do not use no-cache)
  14. header("Cache-Control: must-revalidate");
  15. header("Cache-Control: post-check=0, pre-check=0", false);
  16.  
  17. // HTTP/1.0
  18. header("Pragma: no-cache");
  19.  
  20. // Set the correct response code
  21. header("HTTP/1.1 307 Temporary Redirect");
  22.  
  23. // Redirect the browser
  24. header("Location: http://store1.com/en/");
  25.  
  26. // Make sure that any code below does not get executed when we redirect.
  27. exit;

The Root Folder .htaccess File

The .htaccess file in the root folder

  • turns off directory browsing
  • tells the Apache server to use index.php as the default file
  • activates the Magento http 404 “Not Found” page for the entire site, even outside the Magento folders.
Options -Indexes
DirectoryIndex index.php
ErrorDocument 404 /http404.php

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:

  1. // Default http 404 not found error handler.
  2. // The 404 status is already set here.
  3. // Just call the "not found" page in the default language folder.
  4.  
  5. require "http://store1.com/en/no-route";

The Root Folder Sitemap Files

To learn more about how to create sitemap files with Magento, you may first want to read more about creating Google Sitemaps for Magento sites.

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:

  1. // Link to the sitemap in the Magento directory
  2.  
  3. // Prevent contents from being cached
  4. header("Expires: Fri, 14 Oct 1955 05:00:00 GMT");
  5. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  6.  
  7. // Inform user agent that content is XML and is UTF-8 encoded
  8. header('Content-type: text/xml; charset=UTF-8');
  9.  
  10. // Read sitemap XML file from Magento and render it.
  11. // Do not use http here as Zend doesn't seem to allow it!
  12. @readfile ('/home/magentohome/public_html/magento/store1en_sitemap.xml');

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:

User-agent: *
Disallow: /en/catalog/product/view/id/
Disallow: /en/catalog/product_compare/
Disallow: /en/catalogsearch/
Disallow: /en/cgi-bin/
Disallow: /en/checkout/
Disallow: /en/contacts/
Disallow: /en/customer/
Disallow: /en/newsletter/
Disallow: /en/review/
Disallow: /en/wishlist/

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/
  • /bootstrap.inc/
  • /catalog/product/view/id/
  • /catalog/product_compare/
  • /catalogsearch/
  • /cgi-bin/
  • /checkout/
  • /contacts/
  • /customer/
  • /downloader/
  • /install/
  • /images/
  • /js/
  • /lib/
  • /magento/
  • /media/
  • /newsletter/
  • /pkginfo/
  • /private/
  • /report/
  • /review/
  • /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.

Sitemap: http:/store1.com/store1en_sitemap.php
Sitemap: http:/store1.com/store1de_sitemap.php
Sitemap: http:/store1.com/store1fr_sitemap.php
Sitemap: http:/store1.com/store1es_sitemap.php
Sitemap: http:/store1.com/store1it_sitemap.php

An example of a complete robots.txt file for a Magento site can be found here: http://movingboxesforall.com/robots.txt.

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. This process is documented in the Magento User's Guide.

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:

and the following settings in Admin > System > Configuration > Web > Secure:

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:

cp -avf /home/magentohome/public_html/magento/skin/frontend/ /home/store1/public_html/magento/skin/frontend/
cp -avf /home/magentohome/public_html/magento/media/ /home/store1/public_html/magento/media/
cp -avf /home/magentohome/public_html/magento/js/ /home/store1/public_html/magento/js/

Since all the three directories have been copied to store1.com, you can use the following settings in Admin > System > Configuration > Web > Unsecure:

and the following settings in Admin > System > Configuration > Web > Secure:

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:

cd /home/store1/public_html/
mkdir magento
cd magento
ln -s /home/magentohome/public_html/magento/media/
ln -s /home/magentohome/public_html/magento/js/
mkdir skin
cd skin
ln -s /home/wabnet/public_html/magento/skin/frontend/

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:

and the following settings in Admin > System > Configuration > Web > Secure:

Using symbolic links can possibly impact the security of your websites. If you know more about this, please update this section!




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs