Wednesday 5 August 2009

Catch-all redirect in Apache

Assume you have a website running on an Apache server and the URL is http://mydomain.com/. What if you want to dominate the UK market too!? You'd probably buy mydomain.co.uk and point it to the same server. This is bad! It causes something called Canonicalization - web content that has more than one possible URL. This can lead to massive SEO penalties so my advice is avoid it at all costs.

Instead, have a single URL that serves the content and defer anything else. In this scenario, mycompany.com would be the master and the rest (e.g. mycompany.co.uk) would return a 301 redirect (not a 302). It's also worth mentioning that http://www.mycompany.com/ and http://mycompany.com/ are different (with and without the "www." prefix). If they both serve the same content, Canonicalization will occur. Choose one and stick with it.

The following can be put inside the httpd.conf file to allow a single URL access to a website and redirect the others:

<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot /var/www
</VirtualHost>

<VirtualHost *:80>
ServerName default
ServerAlias *
Redirect 301 / http://mydomain.com/
</VirtualHost>

I'll also do a follow-up post to show how to do the same in IIS.

No comments: