:: TheOneAndTheOnly.com – Andrew Buckman ::

Domain Rewriting

Blogged in Plesk,Web Development by Andrew · Saturday February 28, 2009

I was recently setting up a subdomain for a URL pointing at a shared IP.  The domain itself already had DNS set up for all subdomains to go to the IP, but nothing was being done with them on the server.  In a moment of curiosity, I checked out the subdomain’s URL before I set it up on the server and much to my dismay, it pulled up the primary website set up on that IP, a completely different domain than this subdomain was going on.  This is obviously not good because of duplicate content issues with the search engines, I certainly don’t want an entire copy of my site reachable on an unlimited number of subdomains on other domains.  To combat this, I’ve added an Apache Rewrite directive to my .htaccess file that rewrites anything not on the correct domain over to the proper one using a permanent redirect.

RewriteCond %{HTTP_HOST} !theoneandtheonly\.com
RewriteRule .* http://www.theoneandtheonly.com/ [R=301,L]

Obviously, replace theoneandtheonly.com with your domain of choice.  Please be sure to put this after any other subdomain rewriting rules you might be using.  I don’t intentionally send subdomains to my main URL, if you do, you may need to tweak this a bit.  Also I opted to drop any path information on the rewritten URL, if you’d prefer passing that along, use the version below instead.

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

For the record, I encountered this problem on a server running Plesk 8.2.1, it may or may not be an issue under other setups.

©2010 Andrew Buckman
28 queries. 0.243 seconds.
Powered by Wordpress
theme based on desert by evil.bert