Drupal’s Multi-site Aliasing With sites.php

Drupal’s multi-site capability has been around for quite some time. Site builders are able to spin up multiple sites, using the same code base, by creating a folder in the sites directory that matches the host name and path. On a production server the URLs never change, but if you want to create a local development version of your multi-site setup that’s where things get complicated.

For example, if your site domains are “example1.com” and “example2.com”. All you would have to do is create two directories called “example1.com” and “example2.com”, then create a file called settings.php in those directories and you’re done. You can place themes and modules within the specific directories and they’ll only appear on those sites. Your sites directory should look like the following:

When Drupal gets bootstrapped, it’ll look for a directory in the sites folder which matches the URL. If a folder is found it’ll use the folders settings.php to load the site. Open the default.settings.php in the default folder for a more detailed explanation. The same applies when you create a local development version. If your local URLs are “example1.localhost” and “example2.localhost”, then you’ll also have to create a folder within the sites directory. When you have specific modules and themes within those site directories, managing the codebase with a version control system becomes very painful.

Luckily this issue has been fixed in Drupal 7 thanks to the sites.php. The sites.php is stored in the sites directory, if you have installed Drupal 7 you should see a file called example.sites.php. Rename example.sites.php to sites.php, then uncomment and change the $sites array as needed.

/**
 * Multi-site directory aliasing:
 *
 * Edit the lines below to define directory aliases. Remove the leading hash
 * signs to enable.
 */
  $sites['example1.com'] = 'example1.com';
  $sites['example1.localhost'] = 'example1.com';
Ivan Zugec

About Ivan Zugec

Ivan is the founder of Web Wash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 10 years and has successfully completed several large Drupal projects in Australia.

29 thoughts on “Drupal’s Multi-site Aliasing With sites.php”

  1. im not a techie person. please bear with me. Im on a webserver
    .1. I created the sql database.
    2 a website folder in sites called teg,
    3. copied settings.php without changing it
    4. copied and renameexample.sites.php to sites.php,
    but still getting problem when trying to install drupal… e.g http://www.abc.com/teg/install.php

    Help

    should i have amended the settings.php file and put in the actual name of the database and password, or should i leave it as is?

    when im in the sites file, should it be $sites[www.abc.com/teg’] = ‘teg’;

    holla at me please man

  2. So you are using example2.com, but I dont see that in your $sites variable in your code above? I dont get it. Also, is this aliasing thing got to do anything with vhosts and domain names? Thanks

  3. great site!

    how do you ensure clean urls for multisites during initial drupal 7 install? i know you have to override/overwrite something in /etc/apache, either in the enabled or available sites, right?

    1. Setting up clean URLs is a separate issue to setting up multi-sites. To setup clean URLs just make sure you enable the mod_rewrite apache module.

  4. I need a Drupal 7 website with 2 sections…

    for eg. http://www.example.com is main domain.

    I have no sub domains.
    The two sections of website are One is Civil and the other is MEP.

    We have a Landing page with navigation to the below two sections:

    section 1: http://www.example.com/Civil

    section 2: http://www.example.com/MEP

    The exact requirement is as follows:

    / = intro page

    /site-a/ = menu and templates for site a

    /site-b/ = menu and templates for site b

    Separate layout and menu for these two sections. How can we keep two layout for the two sections? Is it a multisite? or just two sections?

    I think we can use Themekey module for automatic selection of a theme depending on current path.

    Please suggest me a solution.

    1. First of all understand that it is difficult for me to offer a solution because I don’t know the exact requirements for your website.

      Looking at Themekey module is a good start. Also, look at the Domain access module (http://drupal.org/project/domain).

      A multi-site setup is good if you want to share a single codebase, but each site will have their own databases. You can share database tables between sites but I don’t recommend it unless you know what you’re doing.

      1. Hello Ivan,

        I want a Drupal 7 website with landing page and two sections with different templates

        I have a domain xyz.com

        when we try xyz.com, we will see a page with two links…one for a section called Civil and the other is called MEP.

        For both civil and MEP ,there will be different menu items and the layout is different.

        so the link from the landing page may be xyz.com/civil and xyz.com/mep.

        I don’t want sub domains for this.
        Please check this:
        http://drupal.stackexchange.com/questions/70596/drupal-7-website-with-landing-page-and-two-sections-with-different-templates/

        I think this is not a multisite.

  5. What do you mean by “templates” are they 2 separate Drupal themes? If it’s 2 separate themes, then things will get complicated.

    I would keep everything in a single theme and change the design using CSS and template overrides. One of the comments on stackexchange (http://drupal.stackexchange.com/a/70603/247) explains it pretty well.

  6. I have multisite with same codebase installation on my server. One of the site have 5 urls means I have to redirect from all of the five to same site folder(site-1). I have made 1 url as base url in settings.php. I have configured all the five URLs in sites.php to hit on the site folder (site-1). Now when I try to access the site with these 5 urls, I got the website correctly with ist url(base url in settings.php) but when I try to access the site with other 4 urls I got the website content but without CSS. Means CSS is broken in other 4 urls.

    Please suggest any solution.

    Thanks
    Amit

    1. I really can’t answer this without spending time investigating the problem. See if you can get it working with 2 URLs instead of 5.

  7. Hi, I have 2 sites on my server. They are working fine but when I connect to one of them from outside, it is showing the ip address of the server (for example when I hover a link). I configured everything right I think, hosts and vhost files, .htaccess, settings.php and sites.php but I can’t find the problem. Do you have any suggestion?
    Thanks, Linh

    1. *They are working fine but when I connect to one of them from outside, it is showing the ip address of the server (for example when I hover a link).*

      So the links are appearing as an IP, not a domain?

      Try setting the `$base_url` in the `settings.php` file and see if that fixes the issue.

          1. I think I found the problem but I don’t really know how to fix it. The first website that showing the IP uses the settings.php of the default folder. But when I change the $base_url in that file, then the blocks and the CSS don’t work anymore. Any thought?

  8. Hi Ivan!

    I have a multisite configuration but my websites come from repository with a different kind of tree directory..

    Was upload like this:

    —–example1
    |__src
    |__module
    |__themes
    —–example2
    |__src
    |__module
    |__themes
    How can i set to Drupal see theses folders inside “src” ?

  9. can We use same database for two domain like domain1.com and domain2.com
    But I want to share the content in both domain .If i change in domain 1 content . It will show changes in domain2

    But i want to use different layout different menu and different footer and different footer . How can I achieve this ?

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.