Sunday, May 18, 2008

Apache Rewrite Rules

The Apache module mod_rewrite provides a powerful mechanism for hiding, redirecting, and reformatting request URLs. I just finished implementing a mod_rewrite scheme for timfanelli.com to accomplish 3 things:

1. Redirect old URLs with a 301 redirect code
2. Hide certain parts of the URL from my readers.
3. Optimize my Google pagerank.

Using a two simple rewrite rule
RewriteEngine on
RewriteRule ^/index.cgi(.*) /old.html [R=301]

To hide the /cgi-bin/blog.cgi portion of my URL.
RewriteRule ^/blog/(.*)$ /cgi-bin/blog.cgi/ [PT]
RewriteRule ^/$ /blog/ [R=301]
RewriteRule ^/blog$ /blog/ [R=301]

To redirect to domain name to www.domain.name
RewriteCond %{HTTP_HOST} ^yourdomain.com$
RewriteRule .* http://www.yourdomain.com [QSA]