Thursday 24 November 2011

Redirecting from non www to www

For some reason I always tend to forget how to do this, so since I have recently just done this for a site, I thought I would get it down in a blog, as much for my own reference as anything else! Obviously feel free to use if you come across this and it is useful to you :)

As everyone knows search engines will treat http://domain.com and http://www.domain.com as two completely different urls, therefore if they have both indexed against your site you be penalised for duplicate content, and that is not a good thing.

So before Google/Bing and the rest find out about your site, it is best to get this in place in your web config file nice and early (by early I mean from first live deployment onwards!). This config assumes you are running your site on IIS7.



This goes in the <system.webServer> section, and will automatically re-direct anyone coming in without the www in their url, over to the www version without them noticing.

Looking with fiddler (or any other proxy tool where you can analyse http requests and responses) I cannot see any actual 301 statuses, which will not matter if you get this into your config early enough. Search engines (and anyone using links from the site) will only ever know one version so it should be happy days.

I know this nothing new and there are lots of places where it is always documented, but in each case it seems to be slightly different, and this seems to work for my the best so far.

Thanks
Jason