Changing a domain name using mod_rewrite and PHP
Recently at work, we decided that we wanted to change the domain name of one of our customer-facing websites. The website itself would stay the same, content-wise, but we wanted people to start using a different domain name to access it, and use this new domain in some marketing materials. We had lots of users that had bookmarks using the old domain name, and we wanted to be sure those continued to work. Also, we needed to make sure any inbound links from other websites also continued to work.
One easy way to solve those problems is just to set the new domain up as a ServerAlias in the httpd.conf file. That way, anyone using either the new domain or the old domain will get exactly the same content. There are two problems with this however. The first is that users who continue to use the old domain name don't know about the new one (this was important to the marketing folks).
The second problem is what happens to our site when Google or another search engine crawls it. The search engine will crawl the two domains as if they are seperate websites. They don't know which one you want to be the "official" site, so when they presents search results to the user, they may use the wrong domain (or worse, show both). This is even more of a problem if you have, say, 20 domains and you want them all pointing to one set of content.
To solve these problems, we need to send the client an "HTTP/1.1 301 Moved Permanently" header when they access any content using one of the "secondary" domain names. There are many ways to do this; the way I did it uses apache's mod_rewrite and a simple PHP script.
In this example, I'll assume that we want alice.com to be my primary domain name and bob.com and carol.com to be two secondary domains that redirect to alice.com. Here's the relevant section of my httpd.conf:
<VirtualHost xxx.xxx.xxx.xxx:80>
ServerName alice.com
DocumentRoot /var/www/alice.com
</VirtualHost>
<VirtualHost xxx.xxx.xxx.xxx:80>
ServerName bob.com
ServerAlias carol.com
DocumentRoot /var/www/redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ redirect.php?goto=$1 [L]
</IfModule>
</VirtualHost>
We have two virtual hosts set up. The first is the main website serving alice.com. It uses the /var/www/alice.com directory, which is where all of the content of our main site resides. The second virtual host responds to all requests sent to either bob.com or carol.com. The DocumentRoot for this host is /var/www/redirect, but there won't be any of our website content in that directory. The only thing in that directory will be a single PHP called redirect.php.
The single RewriteRule directive has the affect of taking any request for this virtual host and serving up redirect.php instead. The path that was requested by the user gets passed to the redirect.php script in the query string. For example, if a user types in "http://bob.com/contact.html", apache will serve /var/www/redirect/redirect.php?goto=/contact.html.
In the PHP script, we have to send the 301 header, and then tell the browser where the document moved to:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://alice.com".$_GET['goto']);
?>
And that's it. Now when anyone tries to visit a URL on bob.com or carol.com, they will get a 301 header and the browser will redirect them to the correct page on alice.com. Search engines will also update their indexes to reflect alice.com the next time they crawl the site.
