RewriteEngine to change URLs with Apache and .htaccess – 4 easy and common examples

The RewriteEngine modification of the URL allows for many tricks such as readable URLs and maintaining links after site redevelopment. There are many good reasons to clean up URLs into something readable, including having the customer/client remember the link, being able to identify the content by link alone and hiding the underlying technology. Search engines may also place lower priority on content which appears to be generated dynamically.

1) Consistency

To rewrite every page request to have a “www.” prefix, so


 is achieved by:


2) Updating Sites

After redevelopment, containers and directories may change and rewriting is needed to save from breaking old links. To rewrite<code>




3) Make it readable

Guidelines for SEO (Search Engine Optimisation) suggest including the product title in the URL. For example turning <code>products.php?id=12



4) Fix user typos


 is not the same as

 (with added slash on the end, but the user won’t care about nerdy details when they only see an error page.

<code>RewriteRule ^products/([0-9][0-9])$ /products/$1/ [R]

Codemonkey of Node.js, Django, PHP and all things HTML5 Solving tech challenges, one bit at a time.

Posted in .htaccess, apache Tagged with: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *