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.

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.




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs