Drupal coder

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.

Suppose you are developing a site "http://www.example.com". You might then create a settings directory "sites/example.com". On your local machine, you're developing the site as "http://localhost/example.com". Normally, you would need to create a settings directory "sites/localhost.example.com". Now, in Drupal 7 you can tell Drupal to use the same directory, "sites/example.com" for the local copy by setting up a mapping or alias.

To do this, make a copy of "sites/example.sites.php" and name it "sites/sites.php". In there, set up the mapping using the sites array:

  $sites['localhost/example.com'] = 'example.com';

This will instruct Drupal to use the example.com directory for the "http://localhost/example.com".

January 18, 2010Drupal 7, Drupal, multisite

Comments

Only one question about the same in drupal 6. I'm trying to develop a Multisite drupal 6 installation.
I have 2 domains www.domain1.com and www.domain2.com

I've no problem to accessing from outside and from the server itself (editing hosts files and creating the sites/localhost.domain2.com/settings.php file). But how develop from the intranet (private network)?
if I put:

http://server_ip/ only the default site is shown. How to develop the second domain site from another station?

Thanks for your time and perhaps this newbie question...but google don't have any info about this...

Will drupal 7 support single codebase and site database without domain access module and table prefix?

Thanks for sharing this info. This will be very helpful. It took me a minute to grasp it's significance though.

You can follow the full discussion on why this feature was added in the issue queue.

You can still work your own way with with different configuration folders, include files, ... But most people try to have the exact same configuration for development, acceptation and production (maybe using virtual images).

I tend to agree with Christopher. If you have setup multi-stage deployment then your different sites likely require different database credentials. Also change/add different settings in settings.php they'll need to pass through quality assurance so they'll need to start out on your development site, get copied to your Q/A settings.php and then to the production settings.php.

Interesting feature,
Thanks Christopher

Christopher, I typically use a trick like this in my settings.php file to ensure different databases are used depending on whether I'm accessing the site on its testing server or its live server.


if ($_SERVER['HTTP_HOST'] === 'example.test') {
$db_url = 'mysqli://local_u:local_p@localhost/example';
}
else {
$db_url = 'mysqli://remote_u:remote_p@localhost/example';
}

Wouldn't your local dev and remote production sites then be pointing to the same database? Because settings.php is inside the sites folder? Don't you want the same code by and large but possibly some different modules restricted to one site or another, and definitely different settings.php files for each site?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options