Drupal Trick: Multiple Subdomains, One Database

Note: This post is from 2005, I’m not sure what, if anything, is still relevant.

Drupal is an opensource content management system that runs on PHP+MySQL with a flair for community created content, and a robust developer community that offers a lot of capabilities “out of the box,” and even more by adding community-developed modules. I think I first encoutered it a year or two ago, but really started investigating it a few months ago to support a few projects for my employer and since then I’ve been giving it strong consideration for some personal projects as well.

For the past day and a half I’ve been trying to figure out a good way to use Drupal to manage a collection of sites while minimizing administrative overhead.

The worst case is to install a copy of drupal for each site, along with a separate MySQL database for each site. This isn’t difficult, it probably only takes 10 minutes or so per site, but it hurts my brain to think of doing 10 or 15 sites that way because it means maintaining a separate set of users for each and it means that content can’t easily be shared between sites.

Slightly better is to take advantage of Drupal’s ability to have multiple installations use a single database while sharing certain tables between installations, specifically the table of users. This approach reduces some of the administrative overhead, but still has potential problems for sharing content and the like.

Of course, one can go even further with this approach, and share content tables. Unfortunately, it gives me a headache thinking about what would be shared and what would need to be unique to each installation. It also requires tweaking of the install scripts for each new installation. Getting it repeatably correct probably requires creating custom config scripts, and frankly, I don’t have the skills to write them before my lazyness takes hold.

Of course, sharing content tables doesn’t necessarily solve the whole problem anyway, because, by my estimation, once you share enough of the database between multiple installations, you loose the ability to make the sites appear notably different, at least in terms of the content they display.

What I really wanted was an easy way to have a single drupal installation, and then show a different home page to the user depending on the domain name they used to access it. I figured I could use Drupal’s powerful taxonomy features to identify which domains/sites a given piece of content should appear on, but I wasn’t sure how to make it appear properly on the front page of the individual sites. There are suggestions for redirecting a subdomain to a particular page using Apache mod_rewrite, but if I read things right, this results in the loss of the original host name.

I went digging through the drupal site for options. I found a number of people looking to do similar things, and I noticed that future versions of Drupal may have Publish-Subscribe functionality that will be useful for this sort of application, but I didn’t find a real solution for right now that wasn’t going to require more custom coding that I was comfortable with doing myself.

Then I hit upon the solution. Drupal lets you host multiple sites off a single copy of the code by setting up separate settings files for each installation (2nd half of step #4). Drupal lets you configure the default homepage for each site. Usually this is stored in the database, but it can be overridden in the settings file. The solution is to put the two togeather. Make a copy of the main settings file for each site and override the home page to point to a taxonomy view filtered by the category used to flag the content. Now try it out. It seems to work.

So, step by step:

# Install Drupal
# Create a vocabulary that designates where content should appear and enable it for all the content types you want to share between sites (look under “adminster:categories”). If you want content to appear on multiple sites, be sure to check the “multiple select” box.
# Add terms to the vocabulary for each target site.
# Set up DNS aliases for each additional site that points to the main site (I suggest a CNAME record).
# If necessary, configure apache to fulfill requests for the subdomains from the same document root as the main site. If you are using CPanel or another hosting control panel, you might have to “park” the additional domains.
# create copies of the main site’s settings.php (sites/default/settings.php) and place them in “example.com” and the additional site was “zinc.example.com” then you’d need to put a copy of the settings file in “sites/zinc.example.com/”. See the Drupal install instructions for detailed information on naming the directories properly.
# edit the settings.php file for each additional domain:
## Update @$base_url@ to match the name of the additional domain (@$base_url=’http://zinc.example.com’;@ for example).
## Override the front page: @$conf = array ( ‘site_frontpage’ => ‘section/zinc’,);@
# Save the file and try visiting the additional site.

_Note:_ I have installed the pathauto module which provides nice human readable URLs for pages, including taxonomy views. As a result, I can use “section/zinc” to designate the front page, rather than “taxonomy/x/y”. You could also do this by hand configuring things in the path module.

This approach isn’t perfect, but at least this gets me up and running and gives me something to build upon. I hope other people will find it useful as well.

I’d intended to cut bait on this at noon today, and didn’t figure out this little trick until after 5pm, but it’s satisfying to have figured it out.

17 thoughts on “Drupal Trick: Multiple Subdomains, One Database

  1. Marco

    Thanks mate for the advice, after so many Google searches like “subdomain cpanel drupal” and other keyword combinations… looking through forums giving directions on how to do it but not really giving clear cut instructions… or not giving something you were really after like http://drupal.org/node/4810

    Thanks again!

  2. Chuck

    Hey, this is excellent stuff. I’m brand new to Drupal, so it doesn’t all make sense, (still trying to figure out taxonomies), but this is a great start.

    Does this set up a different admin for each sub-site, or is it only the main site which has an admin?

  3. eas Post author

    Chuck, this has a common admin for all the sites, which was one of my goals. I’m not sure how you’d set up a separate admin for each site while still sharing content among them. I think you’d probably want to do something like what I’ve described, and then use taxonomy based permissions to limit people to making changes to just their sub-site. I imagine though that this might be a bit “leaky.”

  4. Steve

    This is a fantastic idea! But I noticed you are using WordPress on your site here. Do you have this working on a live set of domains yet?

  5. eas Post author

    Steve, I haven’t used this approach on any live sites. This entry gets lots and lots of hits though (compared to most of the articles on my site), and from the search terms people are using, it sounds like most of them are trying to do something like I’ve mentioned here.

    Perhaps some of them will post here.

  6. Nikola

    I found you as search result on drupal subdomains and as it seems you are experienced drupal user and while I am at very beginning with it would you like to cooperate and help me build multi-user-subdomain-per-registration-template-driven-portal-featuring shopping cart, web cam chat and if you like would you volunteer or how could be arranged that issue, please email me,
    best regards

  7. Paul

    I am very new to Drupal and I am trying to set up a subdomain in Cpanel for the purpose of self teach Drupal, I have another site in Cpanel thru a host that has let me set up this subdomain on the same account, I have have only gone as far as to name the s/domain and that’s as far as I got, I know i have to set up a data base but I do not know how, what I am after is a step by step guide, for what I read here your are all mostly experienced drupaliers but pointers, url’s of somewhere to go for this sort of very basic help would be gratly appreciated. Thanks in advance

  8. L. Ron Hoover

    I found this page by searching for help on multiple installs. What I’m unsure of is what to put in the new directory that points back to the main install, and the corresponding settings.php file. Is it a mod_rewrite, or what???


  9. Air Line Hot Jobs

    hey ther

    I tried this, but it seems in new drupal it doesnt show up theme …
    this link is a parked domain … on



  10. Air Line Hot Jobs


    it is working .. i had added a trailing slash in settings.php

    now its workign …

    thanks a lot


  11. Dang

    Awesome, just the solution I need after half day test & try other suggestions. Thanks for sharing. Dang.

  12. chony

    I followed the tutorial, and when trying the new site, it show the front page, not the taxonomy one.


    site.com <– main site.. everything Ok
    sub.site.com/category/term <– this one shows the frontpage, not the taxonomy page.

    I cleaned caches, and made shure I wrote site_frontpage correctly in settings.php but still nothing.

    any idea?

    thanks in advance

  13. chony

    I just noticed that it sets the title correclty (the one corresponding to the taxonomy page), but shows the template for the frontpage no matter what. if I go to the URL via de main site, it shows Ok.

Comments are closed.