Multisite and a half twist

What if you want to run Drupal in multisite mode from a subdirectory? Or better yet, what if you want to run several versions of Drupal on your web host - each in multisite mode and each in its own subdirectory? Maybe you like the security of being able to update sites one by one, or maybe one of your sites uses a module that hasn't been upgraded yet.

First, you had better get familiar with the workings of Apache mod_rewrite. Still, all of this can be done from a .htaccess file. Here is a little script to get you started:


# ***** Multi-Multi-Rewrite ***** script by geoff@outbackdev.com.
# *** This script will rewrite http requests for several Drupal multi-sites 
# *** to the proper subdirector(ies). Other requests are not touched.


# ** Place this in your webroot directory and name it .htaccess
# ** Set the host names and subdirectory names below.
# ** All virtualhost documentroot directives can now point to 
# ** your webroot and this script will sort them out.
# ** Each sites base_url can be set: $base_url = 'http://example.com';


RewriteEngine on
# Options FollowSymLinks

# Set drupaldir = empty 
RewriteRule ^ - [E=drupaldir]

# First multi-site: If REQ matches a drupal host, set drupaldir
# Example: RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?192\.168\.5\.1$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^ - [E=drupaldir:subdirectory1]

# Second multi-site: If REQ matches a drupal host, set drupaldir
RewriteCond %{HTTP_HOST} ^(www\.)?somesite\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.localhost$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?mynewsite\.dev$ [NC]
RewriteRule ^ - [E=drupaldir:subdirectory2]

# **** Don't change anything below here! ****

# If drupaldir is set, test for a file and rewrite. 
RewriteCond %{ENV:drupaldir} !^$
RewriteCond %{DOCUMENT_ROOT}/%{ENV:drupaldir}%{REQUEST_URI} -f
RewriteRule (.*) %{ENV:drupaldir}/$1 [NE,L]
# fixes relative links

# If drupaldir is set, everything else is sent to drupal index.php
RewriteCond %{ENV:drupaldir} !^$
RewriteRule (.*) %{ENV:drupaldir}/index.php?q=$1 [NE,QSA]

But wait, there's more! You say that you want to run one of your Drupal sites - say, a registration program - inside a subdirectory of your main Drupal site, like: http://example.com/registration? Or run some other web app like your old bulletin board inside a subdirectory of your Drupal site at: http://mysite.com/oldstuff?

Yes, it can be done with .htaccess files and Apache mod_rewrite. I'll update when I've got it tested. Or contact me if you need it sooner at geoff@outbackdev.com.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.