06 July 2011

If you ever need to redirect one domain name to another (using apache) and you need to do it in code you can simply create a .htacess file in the root of the folder that needs to be redirected and pop the standard 301 redirect in there. That doesn't really help though if you want all the subdirectories of your old domain name to map to the new one, in which case pop the following in:

RewriteEngine On
RewriteRule ^.*$ http://example.com/$0 [R=301,L]
Obviously switch example.com for your domain name and any requests which hit this folder or below will be redirected and keep the subfolders, filename and any arguments that were passed on. You can also do some interesting things with the user agent. For instance, if you want to only forward search engines you can add a rewrite condition such as the following which will only redirect altavista and lycos' search engine bots. This is called cloaked forwarding and is usually against the terms and conditions of the search engines (ie, they will penalise you if they think you're forwarding their bots but not users), so don't do it unless you really hate that user agent!
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} AltaVista [OR]
RewriteCond %{HTTP_USER_AGENT} lycos 
RewriteRule ^.*$ http://example.com/$0 [R=301,L]


blog comments powered by Disqus