Rewriting URLs For SEO

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. http://drupaler.co.uk) get permanently redirected to the correct sub domain (e.g. http://www.drupaler.co.uk). 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.drupaler.co.uk$ [NC]
        RewriteRule ^(.*)$ http://www.drupaler.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.drupaler.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.drupaler\.co.uk$ [NC]
        RewriteRule ^(.*)$ http://www.drupaler.com$1 [L,R=301]
        RewriteCond %{HTTP_HOST} ^drupaler\.co.uk$ [NC]
        RewriteRule ^(.*)$ http://www.drupaler.com$1 [L,R=301]

Add as many TLDs as you have/want. The above pushes drupaler.com, drupaler.co.uk *and* www.drupaler.co.uk to www.drupaler.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 Menus & Blocks' own James Panton for assistance with this.

Post new comment

© 2010 Greg Harvey. Drupal theme by Kiwi Themes.