Multi-site compatible apache rewrite rules

When using multi-site configurations in Drupal 4.7, and you want to use URL rewriting in .htaccess to force 'www.' or non-'www.' subdomain access, the rewrite rules used in the standard .htaccess file may be problematic.

# If you want the site to be accessed WITH the www. only, adapt and uncomment the following:
# RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
# RewriteRule .* http://www.example.com/ [L,R=301]

#
# If you want the site to be accessed only WITHOUT the www. , adapt and uncomment the following:
# RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
# RewriteRule .* http://example.com/ [L,R=301]

This is due to the fact that the condition they are testing for is: "When the HTTP_HOST header is NOT www.example.com (or example.com in the latter example), rewrite the url with www.example.com (or example.com)"

Using this method will cause you trouble if you have two or more sites running on the same code base in 4.7, because, the HTTP_HOST header will trigger an erroneous match and will rewrite the URL to one of the other sites.

For example, if you have two domains, tom.com and jerry.com, both running off the same code base and you want to force both to use the non-www URL form, you should use the following .htaccess rewrite entries:

RewriteCond %{HTTP_HOST} ^www\.tom\.com$ [NC]
RewriteRule ^(.*)$ http://tom.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.jerry\.com$ [NC]
RewriteRule ^(.*)$ http://jerry.com/$1 [L,R=301]

Another advantage to the above form, is that the URL will be rewritten to include the URL path after the hostname, so someone who tries to go to http://www.tom.com/node/123 will get a 301 redirection to http://tom.com/node/123.

Resources

Posted by: Mike on Tue, 10/17/2006 at 11:19pm

Post new comment

This helps us decide if you are a human, and not just some visiting bot.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <h3> <h4> <br> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
More information about formatting options