Drupal coder

multisite

Drupal 7 multisite improvement : multi-site directory aliasing

Using Drupal's multisite feature, one can use a single codebase for multiple sites. This leaves a smaller footprint and makes your sites more maintainable since you only have to update one bunch of files.

This was already possible in versions prior to Drupal 7 by creating a configuration directory whose name was based on the site's hostname and pathname.

Although easy to setup this was sometimes cumbersome to maintain if you had different copies of your site running on different locations. During the creation of a site, one might typically maintain a development copy, an acceptation copy and a production copy. All these are on different locations. This would lead to creating several versions of the settings directory for one particular site.

In Drupal 7 a solution is provided for this particular problem. A new configuration file is available under the sites folder, called "example.sites.php". Using that file you can set up directory aliases. To see what those are, let's look at a particular example.

January 18, 2010Drupal 7, Drupal, multisite

Drupal multisite in subfolders

We all know you can run several Drupal sites using a single codebase. This is done using Drupal's multisite feature. Most of the time you want to host all sites on a different domain name. There's a lot of documentation on how this can be accomplished.

But sometimes you want to run the sites in a subdirectory on one domain. You may want to do this to run a development server for a multisite. You can host all sites in a subdirectory then. This can be easily done with some extra Apache configuration.

Suppose your domain name is mydrupalsite.com. You want to host a bunch of site using Drupal's multisite feature in the subdirectories


http://www.mydrupalsite.com/subdir1
http://www.mydrupalsite.com/subdir2
http://www.mydrupalsite.com/subdir3

You have to do 3 things.

Add alias your Apache configuration file

We want requests for the 3 subdirectories to go to the same Drupal instance. We can do this using Apache's Alias functionality.

Alias /subdir1 /var/www
Alias /subdir2 /var/www
Alias /subdir3 /var/www

I'm supposing here Drupal's codebase is hosted in /var/www on your machine.

Redirecting all requests to index.php

Now that we're serving all requests from one codebase we have to redirect all requests to index.php. This needs to be done since all Drupal requests are served from one endpoint, called index.php.

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

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

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

Creating our settings.php

You can now have different settings (database, ...) for each site by creating a different settings file for each site. Place the settings.php for each file under a directory called after the domain name and subdirectory. In our example we would have the following setting files:


/var/www/sites/mydrupalsite.com.subdir1/settings.php
/var/www/sites/mydrupalsite.com.subdir2/settings.php
/var/www/sites/mydrupalsite.com.subdir3/settings.php

January 12, 2009Drupal, multisite