Saturday, November 20, 2010

Basic Apache Rewrites

URL rewriting is a powerful technique not just for the Webmasters, but also for the SEO and Security guys. This article will give an overview towards the most common ways to implement rewrites using the Apache mod_rewrite.

A basic rewrite will look like this:

RewriteRule ^(.*?)/abc(.*) $1/def1$2 [R=301]

So for example if you try to access you will be redirected to

The R=301 flag tells Apache that the rewrite is a Permanent (301) Redirect. Furthermore, $1 substitutes the first regex pattern, $2 the second regex pattern, and so on.

If you want to redirect specific requests made by Googlebot or Bingbot (the crawlers used to index pages on Google and Bing search engines), you can use the following code:

RewriteCond %{HTTP_USER_AGENT} .*(Googlebot|Bingbot).*
RewriteRule ^(.*?)/abc $1 [R=301]

This is useful if you want to omit specific pages from being indexed (such as administrator or user profiles). In this case if Googlebot or Bingbot try to access they will be redirected back to

Another popular use of URL rewriting is to make SEO friendly URLs. As you may know, websites in the form are not ranked properly by Google as they lack keywords. A more SEO friendly URL would be since the URL name contains keywords specific to the content of the page. In order to convert URL variables to SEO friendly names, one can use the following code:

RewriteCond %{QUERY_STRING} ^uuid=123$
RewriteRule ^(.*?) $1/123? [R=301]

This will redirect requests from to
Post a Comment