Friday, 28 August 2009

Redirect Using Friendly 301

The .htaccess file can seem daunting, especially when it comes to implementing 301 search engine friendly permanent redirects.

Not any longer.

Why implement them in the first place, well it's better to have your website indexed under one canonical name than two or even three.

How does this come about? In your root folder the homepage is normally labelled 'index.html, index.php &c, so what happens is this, when you click on the homepage link of your website instead of reading: www.mywebsite.com, you get: www.mywebsite.com/index.html; Google also sees this and actually indexes the two differently, even though they are exactly the same pages. This isn't so bad until your back-links start to get affected - there's nothing worse than having the /index.html page hogging more of the links than the .com only page.

But that's not all, it never would be with the web; what about sites having two TLDs (top level domain), for example: .co.uk & .com, you want the search engines to index both of these the same don't you? Likewise when someone links back to your site without the 'www', I presume you want these also to count under your one chosen TLD

Now I'm no programmer, but I have managed to implement the said 301 redirects using a really simple bit of code that you pop into your .htaccess file. I say simple, all three use 'regular expressions', but you don't really have to do the tutorial, just copy and paste.

But as with all changes to your root folder, do ensure you've saved the lot before you carry out any changes;-)

301 redirect .co.uk to .com


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

You only have to type in the 'RewriteEngine On' once.

301 redirect non www to www


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

301 redirect index.html to just the domain: www.mywebsite.com


RewriteCond %{THE_REQUEST} ^.*\/index\.html?
RewriteRule ^(.*)index\.html?$ http://www.mywebsite.com/$1 [R=301,L]

Remember to replace the 'mywebsite' with the name of your website.

End product, after implementing all three 301 permanent redirects, you've a website rendering under one roof that has nothing to confuse the search engine spiders or your potential customers with.