mod_rewrite – An Introduction to URL Rewriting

by Stuart McHenry May 11, 2009

mod_rewrite is a powerful URL Rewriting Engine for the Apache web server. Although available in varying forms in other server software, this post will focus on the Apache rewriting engine.

What is URL Rewriting?

In short, mod_rewrite allows for the server to respond in a specific way based on conditions met by the URL. This allows for many possibilities, including the creation of user-friendly URL schemes for your dynamic scripts.

For example, if you have a PHP article script which requires an article id in the URL, this would typically be accomplished by:

http://example.com/news.php?id=2

However, mod_rewrite can turn that URL into the following:

http://example.com/news/2/news-article-title

The rewritten URL is much more meaningful to users and search engines. The URL is far more memorable without the PHP extension and id request. The article title provides search engines with more keywords, which can help with your search ranking. These benefits become even more apparent as more complicated URLs are rewritten into friendlier forms.

Getting Started

mod_rewrite is used by adding rules and conditions to a file named .htaccess in a site’s directory. The rewrite engine must first be turned on. Open or create a .htaccess file and add the following:

RewriteEngine on

A basic URL rewrite can be performed by adding a Rewrite Rule, formatted as follows:

RewriteRule pattern substitution [flags]

The pattern is a regular expression (regex), matched to the current URL. The substitution is the replacement or change made to the pattern matched URL. Flags are optional arguments which modify the way the rewrite engine acts.

To implement the rewrite example above, the following code is used:

RewriteEngine on
RewriteRule ^/news/([0-9]+)/(.+)(/)$ /news.php?id=$1 [L]

The pattern is started with ^, and ends with $. The brackets represent groups, the first matches any number of digits, the second matches any number of characters, followed by an optional trailing slash. The substitution tells the server to fetch news.php, with an id of the first group in the pattern, $1. The L flag tells the rewrite engine that this is the last rewrite to be performed.

Conditional Rewriting

To further expand control over the rewrite engine, Rewrite Conditions can be set. These allow for conditions to be matched before a Rewrite Rule is to be executed. The following syntax is used:

RewriteCond TestString CondPattern

The TestString can be one of a number of system variables, such as the current referrer or browser user agent. The CondPattern is again a regular expression, which will be matched against the TestString. A useful application to a rewrite condition is to check if files or images are being hotlinked.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC]
RewriteRule ^.(gif|jpg|png|bmp)$ http://example.com/donothotlink.jpg [R,L]

The conditions check if there is no referrer, or if it is from a different site. If one of those conditions are met, the rule checks if the file being accessed is an image. If so, the user is redirected (using the R flag) to an image explaining that the requested image was hotlinked.

Other Possibilities

Some common uses of mod_rewrite include; redirecting to another page based on browser user agent, moving a site to another domain, forcing the addition or removal of www. on your domain, etc.

Although mod_rewrite can be difficult to learn and master, the possibilities are endless.

Further Reading and References

Official Apache mod_rewrite Documentation
Added Bytes mod_rewrite Cheat Sheet

Stuart McHenry
Stuart McHenry is a US-based SEO Consultant focusing on link building, content marketing, local SEO, and reputation management. Follow Stuart on Twitter @smindsrt

Leave a Reply