Redundant Domains + Apache + ModRewrite = Redundant URL’s 

How to make any url work with 2 different domains. In one easy step.


I recently bought a second domain for myself. It was a whimsicality because I already owned a great top-level domain (tiagomachado.pt), but since tiagomachado.com was for sale (bloody cybersquatters), and .com is still the holly grail of TLD’s, I managed to pay the $350 being asked (initially were $500, but after some bargaining I managed to drop $150 on the price).

All my domains are hosted on a (gs) Grid Service (shared hosting) at (mt) Media Temple. They have a good knowledge base, and have the Apache mod_rewrite module well documented, but they lack an example to convert one domain to another (we’re not talking about a simple “url redirect”). My main goal was to make every single url within tiagomachado.pt also work with tiagomachado.com (although the “final” url would always be on the tiagomachado.pt domain, since the files are hosted there). If someone by accident switch the .pt for .com they still find the file. It’s what redundancy is all about: to prevent errors and make users’ life easier.

So, my primary domain will be tiagomachado.pt, and my secondary domain will be tiagomachado.com. Every user following a link with tiagomachado.com will end up on tiagomachado.pt. If you try to access http://tiagomachado.com/files/Designing-for-Credibility.pdf you will end up on http://tiagomachado.pt/files/Designing-for-Credibility.pdf. It is as simple as that.

On Media Temple, by default, the root of a domain has two folders (cgi-bin and html) and a .htaccess file. You should place the following instructions inside the .htaccess file (if you don’t have one create it), inside the html folder of the secondary domain (in my case tiagomachado.com):


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

My own example:

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

Have fun with your (much better) redundant url’s! Kudos to Henrique Mouta for helping me out with this.

Note: If you find spelling or grammatical errors it’s normal, I’m not a native english speaker. But please feel free to email me at tiago@playout.pt with any ideas, questions or comments. I usually reply within 24 hours (during weekdays).

Email me when Tiago Machado publishes or recommends stories