Quick and Easy 301 for Parked Domains and Dub/Non-Dub

I’ve been asked a couple of times about this recently, so thought I’d share it here.

The two most common times I have to use 301 redirects are in the prevention of potential duplicate content issues. The first is the old non-www to www (or vice versa) chestnut and the second is to prevent parked domains from looking like seperate sites.

There’s a nice little bit of code that can handle both of these, without filling up your .htaccess file:

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

Basically we’re checking to see that URIs begin with www.pixelpunk.co.uk – if it does, all fine and dandy, our work here is done. If they don’t, we redirect to www.pixelpunk.co.uk/whatever_else_they_might_have_entered.

As you can see this handles both a request to pixelpunk.co.uk, which is missing the www, and any parked domains, say puxelpink.co.uk.

As always with mod_rewrite – your particular needs may mean you need to handle the two bits seperately, but I find this a good one-size-fits-all for around 70% of the sites I work with.

Neat eh?