One of the many motivations for starting this blog was to finally use a domain name I have been sitting on since 2005 (2005!). Since then, my taste in domain names has changed, but I don’t have the heart to relinquish that first domain. Enter Apache’s mod_rewrite rules. By my modifying the .htaccess file that enables fine-grained rules for how your site content is accessed, a server will correctly respond to/rewrite multiple domain names, assuming your httpd.conf file and A records are set correctly.

Here’s my first cut at writing these rules. I wanted all of my domain names with a non-.com extension to permanently redirect to the domain with a .com extension. The domain name with a .com extension is where WordPress thinks my site is located, so this should be the canonical URL.

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

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

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

Let’s break these rules down into plain speech:

  • If someone asks for mydomain.net or www.mydomain.net (ignore case), send them to mydomain.com. Tell the requestor that this url is permanently redirecting. Don’t process any more rules.

  • If someone asks for mydomain.org or www.mydomain.org (ignore case), send them to mydomain.com. Tell the requestor that this url is permanently redirecting. Don’t process any more rules.

  • If someone asks for mydomain.biz or www.mydomain.biz (ignore case), send them to mydomain.com. Tell the requestor that this url is permanently redirecting. Don’t process any more rules.

See how these rules are repetitive?

Instead of having three rules that do pretty much the same thing, I thought would look for a way to combine rules. The solution I used is inspired by regular expressions. Though some may say that regular expressions turn one problem into two, here I found it both readable and helpful. Notice that the first cut example has a pattern to account for the presence or absence of a www prefix. Could I make a pattern to account for different extensions?

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.(net|org|biz)$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]

This says:

  • If someone asks for mydomain with or without a www in front and with .net, .org, or .biz at the end (ignore case), send them to mydomain.com. Tell the requestor that this url is permanently redirecting. Don’t process any more rules.

This looks cleaner to me. If you have multiple domain names and extensions associated with your box (like I do), imagine how much redundant code you could avoid :)

More reading:

http://httpd.apache.org/docs/current/mod/mod_rewrite.html http://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/ http://www.noupe.com/php/10-mod_rewrite-rules-you-should-know.html http://www.workingwith.me.uk/articles/scripting/mod_rewrite