Rewriting URLs For SEO

How to avoid duplicate content by making sure that any requests to no sub domain get permanently redirected to the correct sub domain.

Photo of Greg Harvey
Fri, 2009-12-18 16:52By greg

Quick non-Drupal, but very useful, post here. There are lots of people talking about things like this on the 'net, but I've never found my specific requirements in one place. So here they are, in one place. I'm sure other UK web developers will need them!

This is about avoiding duplicate content by making sure that any requests to no sub domain (e.g. https://codeenigma.com) get permanently redirected to the correct sub domain (e.g. https://www.codeenigma.com). This way Google doesn't see duplicate content and we're like three little Fonzies, Drupal, Google and me.

The re-write for that is widely available in various forms - I'm using this one - just stick it in your site's vhosts entry in the Apache config files:

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www.codeenigma.co.uk$ [NC]

RewriteRule ^(.*)$ https://www.codeenigma.co.uk$1 [L,R=301]

Note, if you don't know where the Apache config files are, pay someone who does or start reading the Apache docs! =P

Ok, so that's cool. BUT what if I also own drupaler.com? What if I want to make sure that all requests end up at www.codeenigma.com, even the ones to the UK TLD? Then it looks something like this:

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTP_HOST} ^drupaler\.com$ [NC]

RewriteRule ^(.*)$ http://www.drupaler.com$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www. codeenigma\.co.uk$ [NC]

RewriteRule ^(.*)$ http://www.codeenigma.com$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^codeenigma\.co.uk$ [NC]

RewriteRule ^(.*)$ https://www.codeenigma.com$1 [L,R=301]

Add as many TLDs as you have/want. The above pushes codeenigma.com, codeenigma.co.uk *and* www. codeenigma .co.uk to www.codeenigma.com. Hurrah! We're all SEO friendly. (On the Apache end at least - you still have a little work to do in Drupal.)

Thanks to coleage James Panton for assistance with this.