Web design, programming, graphics, and pretty much anything else I care about.

Drupal multisite configuration

Drupal can do the following:

www.example.com
site1.example.com
site2.example.com
www.example.com/site3
www.example.com/site4

All these sites can use the same Drupal installation. The key is to point all of them at the same index.php file, which is in the root of the Drupal installation. This requires either a symbolic link to connect a site sub-directory to the installation directory, or Apache modification to do it. Once the request is received by Drupal, it will know which site to retrieve. /smoke and mirrors

To make the sub-site work, you have to add a directory to the sites directory of the Drupal installation. The directory name has to be exact. For site1.example.com, the directory name will be /sites/site1.example.com. For www.example.com/site3, the directory will be /sites/www.example.com.site3. In each directory you can create themes and modules folders and add themes and modules that will be specific to this site. Anything in the /sites/all directory should be applied. Next, in the new site directory you have to put a copy of the settings.php and it has to be customized for the new site.

The important settings.php lines are the following:

$db_url = 'mysql://user:pw@localhost';
$db_prefix = ;

If the sub-site is using a different database, or database user, it has to be configured here. If a different database prefix is used, it is added here. The database prefix tells Drupal to create tables in the drupal database with the supplied prefix. For example, the base installation will have a table called "node" but if the prefix "site1" is used for the sub-site, Drupal will create a node table for that site called "site1_node". This is why {} is used in SQL queries. It allows prefixes to work with the same code. Thus, "SELECT * FROM {node}" will work for any site. Drupal knows that if you are in site1 that this query applies to the site1_node table, and not the node table which supplies the main site.

It is possible to create the following multi-site arrangement.

www.example.com/site
www.example.com/site/separatesubsite

NOTE: It may be necessary to create a symbolic link pointing to the main Drupal installation, when using sub-directories. In the above example, if www.exampl.com/site contains the Drupal install, a link may need to be added inside the site directory to point separatesubsite/ to the directory where index.php is located. If not, 404 not found is returned because Apache can't find /site/separatesubsite. Much of the confusion is created when changing the document root, or using Virtual Hosts.

The logic is easier to understand if the document root is left as is (/var/www/html.) The Drupal install may be /drupal, and other folders exist called site1, sites2, site3, etc. Each is actually a link pointing to /drupal. In the Drupal installation directoy sites/ there will be /www.example.com.site1, /www.example.com.site2, /www.example.com.site3. Drupal will know what to do with these sub-sites, but the trick is getting Apache to point to the correct location. So when using the address http://www.example.com/site1, you are sent to /drupal. Once getting to the right place, Drupal knows you came from www.example.com/site1 and acts accordingly.

Drupal's search order is as follows:

* For example, for a fictitious site installed at
* http://www.drupal.org/mysite/test/, the 'settings.php'
* is searched in the following directories:
*
* 1. sites/www.drupal.org.mysite.test
* 2. sites/drupal.org.mysite.test
* 3. sites/org.mysite.test
*
* 4. sites/www.drupal.org.mysite
* 5. sites/drupal.org.mysite
* 6. sites/org.mysite
*
* 7. sites/www.drupal.org
* 8. sites/drupal.org
* 9. sites/org
*
* 10. sites/default

So Drupal is smart enough to know to go to www.example.com.site.separatesubsite in the /sites folder, before going to www.example.com.site.