Multistore Magento Configuration

There are two elements to making a multi-store environment,

  1. Ensure all stores access a common document root (ie. the same Magento installation).
  2. Set the appropriate mage_run_code and mage_run_type for the given store

There are many ways of actually accessing the different stores in a multi-store environment, you could use

  1. Different domain names per store (eg. www.example.com and www.another-example.com)
  2. Different sub-domains per store (eg. www.example.com and fr.example.com)
  3. Different sub-directories per store (eg. www.example.com and www.example.com/fr)

Sharing the document root

Once you've picked and created an access method (domain, sub-domain or sub-directory), the next step is to share the document root between the stores. There are two common ways of doing this,

  1. Identical document root This is where the entire document root between both stores can be exactly the same.
  2. Different document root This where some of the document root may differ between stores. The most common reason to use this is if you need to present content on the same URI (eg. /blog) on both stores, but each should point to a different WordPress installation.

Identical document root

This is achieved by use of a relative symbolic link (symlink),

Example: Different domain

There are two domains being used,

  • www.example.com
  • www.another-example.com

The main Magento document root is located at /microcloud/domains/example/domains/example.com/http - and www.another-example.com is a slave of this installation. To tie the two root directories together is as simple as creating a symlink from www.another-example.com to www.example.com

cd /microcloud/domains/example/domains/another-example.com/
rmdir http
ln -s ../example.com/http .

Example: Sub-domain

There is one domain, with two sub-domains being used (www and fr),

  • www.example.com
  • fr.example.com

The main Magento document root is located at /microcloud/domains/example/domains/example.com/http - and fr.example.com is a slave of this installation. To tie the two root directories together is as simple as creating a symlink from fr to http

cd /microcloud/domains/example/domains/example.com/
ln -s http fr

Example: Sub-directory

There is one domain, with a sub-directory being used,

  • www.example.com
  • www.example.com/fr

The main Magento document root is located at /microcloud/domains/example/domains/example.com/http - and www.example.com/fr is a slave of this installation. To tie the two root directories together is as simple as creating a symlink from /fr to http

cd /microcloud/domains/example/domains/example.com/http
ln -s . fr

Different document root

This is slightly more complex, as you need to create symlinks, or copies of files as necessary depending on your needs. There is no right or wrong method, or one-size-fits-all method as this is usually used in specific use cases where you know what should be shared and what shouldn't.

The premise is still the same as an identical document root, in the sense that you use relative symlinks to share identical content.

Example: Different domain with different /blog

There are two domains being used,

  • www.example.com
  • www.another-example.com

The main Magento document root is located at /microcloud/domains/example/domains/example.com/http - and www.another-example.com is a slave of this installation - but the /blog URL must point to a different WordPress installation. To tie the two root directories together, symlinks must be created for all Magento common files, except the blog directory.

cd /microcloud/domains/example/domains/another-example.com/http
for filedir in app errors includes index.php js lib media shell skin var; do ln -s ../../example.com/http/$filedir . ; done

Setting the run codes

You can either set the run codes using MageStack environment variables, or you can set them by hardcoding lines in your index.php - we prefer using the former as it is much cleaner.

Example: Set environment variables for a domain

In the domain name's respective ___general/example.com.conf file, set the following variables, changing the values as appropriate

set $my_mage_run_code "fr";
set $my_mage_run_type "store";

Example: Set environment variables for a sub-domain

In the domain name's ___general/example.com.conf file, set the following variables, changing the values as appropriate

if ($host ~ (fr)\.example\.com) {
  set $my_mage_run_code "fr";
  set $my_mage_run_type "store";
}

Example: Set environment variables for a sub-directory

In the domain name's ___general/example.com.conf file, set the following variables, changing the values as appropriate

location ~* ^/(?<store>(fr)) {
  location ~* .(php)$ {
    set $my_mage_run_code "$store";
    set $my_mage_run_type "store";
    include fastcgi_params;
  }
  try_files $uri $uri/ /$store/index.php$is_args$args;
}