Spilling the Beans
Thursday, June 3rd, 2010 at 1:28 pm
Morten Kaltoft (@owix) informed me of problems with my short domain name setup, Google punishes double content posting, and it appeared I was doing so.
After some searching, i found a helpful post by Gruber. Now any request on the short domain “cph.cc” will redirect to the longer “copenhagencocoa.com” with a proper 301 response. It took some time to get working, since I have never messed with mod_rewrite before, nor regular expressions, if you are experiencing similar problems for your domain, you can add this at the top of your .htaccess file:
#Rewrite short domainRewriteEngine OnRewriteBase /RewriteCond %{HTTP_HOST} ^(www\.)?shortdomain\.com$RewriteRule (.*) http://longdomain.com/$1 [R=301,L]
A run through of the code might be in order, the first two lines are mandatory, the third line is the condition for the execution of the fourth line.
‘%{HTTP_HOST}’ fetches the domain the user requested, if it matches the appending regular expression, the fourth line will be executed.
The appending regular expression requires some explanation. The ‘^’ signifies the beginning of the expresion. The ‘(www\.)?’ signifies that the expression will return true regardless of a prepending ‘www’.
‘shortdomain\.com’ describes the domain we are trying to match, and the concluding ‘$’ ends the regular expression.
The reason we write ‘\.’ instead of just plain ‘.’ is that the dot-character is reserved in regular expressions, it signifies any single character, by prepending the backslash we ensure that we are only looking for the dot-character.
The forth line will replace the entire URL ‘(.*)’ with the the new domain, the $1 inserts the tail of the URL (anything after the TLD). ‘R=301′ sends the proper HTTP response (moved permanently), and the L ensures we do not execute any following rules, this is to avoid daisy-chaining rule execution, if other rules are present in the .htaccess file.
1 Comment:
Write a Comment: