Tuesday, 15 May 2012

301 redirect TSOHost Apache

When creating a new website you often find that their established pages will be ranked in Google (or other search engines!) and already have Google Juice or equivalent.  You never want to lose this Juice or have your customers clicking a ranked link and being presented with a nice 404 error.  To get round this you have a couple of options but for my TSOhosts Umbraco setups I implement a 301 htaccess RewriteRule with mod_rewrite enabled.

Here is a simple example of an .htaccess file placed in my public_html (root) folder:
RewriteEngine On
RewriteRule ^contact\.htm /contact-us.aspx [R=301,L]
This is checking for any request that comes in for contact.htm (notice the backslash to escape the "." before htm) and redirects the user straight on to contact-us.aspx. 

The '^' is referred to as an anchor and when used means that the character(s) to the right of it must be the very first character(s) example:
^contact\.htm would match contact.htm but not nocontact.htm.  
You can also do the same with the last character(s) by inserting a '$'.

Don't forget that you have to have mod_rewrite enabled to make this work!


No comments:

Post a Comment