Multi-site

Last modified: May 1, 2009 - 01:49

Multi-site is a very powerful feature in large sites or hosting operations but is often mis-understood. Multi-site is designed as a way to share the same code base for multiple different Drupal based sites. These sites are separate and do not share content or administration. Multi-site just helps with managing the code base, shared themes and modules.

A single Drupal code base installation can host several Drupal-powered sites, each with its own individual configuration and database.

Additional site configurations are created in subdirectories within the 'sites' directory. Each subdirectory must have a 'settings.php' file which specifies the configuration settings. The easiest way to create additional sites is to copy the 'default' directory and modify the 'settings.php' file as appropriate. The new directory name is constructed from the site's URL. The configuration for www.example.com could be in 'sites/example.com/settings.php' (note that 'www.' should be omitted if users can access your site at http://example.com/).

Sites do not have to have a different domain. You can also use sub domains and subdirectories for Drupal sites. For example, example.com, sub.example.com, and sub.example.com/site3 can all be defined as independent Drupal sites. The setup for a configuration such as this would look like the following:

sites/default/settings.php
sites/example.com/settings.php
sites/example.com.site3/settings.php
sites/sub.example.com/settings.php
sites/sub.example.com.site3/settings.php
sites/www.sub.example.com.site3/settings.php
sites/www.sub.example.com/settings.php

If you are installing on a non-standard port, the port number is treated as the deepest sub domain. For example:
http://www.example.com:8080/ could be loaded from sites/8080.www.example.com/. The port number will be removed according to the pattern above if no port-specific configuration is found, just like a real sub domain.

Each site configuration can have its own site-specific modules and themes in addition to those installed in the standard 'modules' and 'themes' directories. To use site-specific modules or themes, simply create a 'modules' or 'themes' directory within the site configuration directory. For example, if sub.example.com has a custom theme and a custom module that should not be accessible to other sites, the setup would look like this:

sites/sub.example.com/settings.php
sites/sub.example.com/themes/custom_theme
sites/sub.example.com/modules/custom_module

Installing Multi-site

  1. Create your new site directory in /sites folder
  2. Place a new, blank settings.php file in the directory.
  3. Edit the appropriate settings in your web server to point to the Drupal root directory
  4. Browse to the site to see the install script, proceed to step 2 of the install.txt.

If one of your sites appears to be in a subdirectory of your domain, you will need to add a rewrite rule to .htaccess to enable clean URLs for it. For example, if you have one site at www.example.com and another at www.example.com/sub, add the following lines to .htaccess before the existing rewrite rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/sub/(.*)$
RewriteRule ^(.*)$ /sub/index.php?q=$1 [L,QSA]

Additional Reference
How To: Setup of /sites directory for multi-site

What about the database? New or old settings.php?

LeeHunter - June 24, 2008 - 13:54

This procedure should say whether or not you need to create a new database for each site. Also, at the beginning it says to copy the settings.php file and modify the file "as appropriate" but later it says "Place a new, blank settings.php file in the directory". Do you copy the old one and change it or do you just make a blank file?

If you do need to edit the old settings.php file, can we be more specific about what changes have to be made?

Symlinks for subdirectories

gaele - July 17, 2008 - 08:31

When using multi-site, if one of your sites appears to be in a subdirectory of your domain, you will need to add a symlink to that directory from the Drupal installation directory. Like this:

ln -s . subdir

where subdir is the name of your subdirectory.

You must map subdomain OR subdirectory to base site

cozzi - September 27, 2008 - 02:51

In order to get the above instructions to work you must:

For the subdomain config:
sites/sub.example.com/settings.php (where you are looking for the url result being "sub.example.com")

You must create the subdomain sub.example.com on your hosting server and then point it to the base site "example.com"

For the subdirectory config:
sites/example.com.site3/settings.php (where you are looking for the url result being "example.com/site3")

You must create the subdirectory example.com.site3 on your hosting server then map that directory to the base site "example.com"

 
 

Drupal is a registered trademark of Dries Buytaert.