Notes: .htaccess

I recently had to switch domain names for a site I run at work. Along with the domain switch, I migrated three Drupal instances to a single Drupal multi-site instance. The basic domain switchover and Drupal site migration went just fine. The difficulty came a few days later when I realized that our payment system was still referencing our old domain (bad!).

Unfortunately, the payment system is a black box in another department that only 1 or 2 people in the entire university are able to fiddle with.  This means that the likelyhood of getting the URL updated in a reasonable amount of time was small. So I did the next best thing: setup a .htaccess rule.

Now, our site(s) are running on a GoDaddy Virtual Dedicated Server, so I basically have free run of the server and can do whatever I want. It’s configured in such a way that every domain that the server uses is given its own system user with its own docroot. What I had done when I originally setup the server is to point the various domains’ docroots to the general one using symlinks. So for /home/domain1, its docroot would have been public_html->/var/www/main_docroot. Same with /home/domain2, a symlink was written to point the public_html directory to /var/www/main_docroot.

The band-aid I intended to apply to get the old URL to point to the new one was to create a rewrite rule in the docroot’s .htaccess file. A .htaccess file is one of the very first thing the Apache webserver looks at during processing for a webpage. A rewrite rule would examine the incoming URL and point it wherever I wanted it to go. Simple, right?

So, as I had mentioned before, during this transition I migrated 3 Drupal sites into a single Drupal instance, configured as a multi site. I won’t go into the details of creating a multisite here because there are plenty of good tutorials around the web. Drupal adds a bunch of stuff to the .htaccess file in Drupal’s home directory, wherever that may be. I opened up the .htaccess file and based on the tutorials I had read, added the following  directives to the file:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC]
RewriteRule ^(.*) http://newdomain.com/$1 [R=301,L]

This should have found any request coming from olddomain.com and taken any parameters associated with it and redirected the request to newdomain.com, applying the found parameters. This series of rules sort of worked. It would take the request and point it to newdomain.com. BUT. Since I’m dealing with a Drupal multi-site configuration, Drupal takes over the processing once it gets down to that level. So the URL including the physical subdirectory, ie olddomain.com/classes would redirect properly to newdomain.com/classes, but the URL olddomain.com/energy which matches the symlink ‘energy‘ would be rewritten to just newdomain.com instead of newdomain.com/energy. This was a pretty big problem because although the payment system would work correctly, anyone using old bookmarks would be taken to the wrong location.

After a day or so of trial or error, I finally stumbled on a mod_rewrite cookbook on the Apache documentation site that referenced ‘moved document root’. Essentially, this is what happens in my circumstances. I went into the old domain’s user directory and deleted the symlink pointing the docroot to the /var/www/main_docroot. I recreated the public_html folder and added a new .htaccess file. I modified the documentation example and put it into the .htaccess file. I tried it out and voila! It worked! All is well with the work again. Here’s my final directive to point all requests to olddomain.com to newdomain.com:

Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]

I hope this helps someone down the road who has a similar problem to solve as I did!


One thought on “Notes: .htaccess

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s