Rewriting URLs with mod_rewrite
October 3, 2010
mod_rewrite is a module for the Apache Webserver. One of its most popular features is rewriting lengthy urls into a shorter, more user-friendly form.
Example
- Using php, your webpages are called similar to the following scheme:
www.example.com/index.php?page=help - Now with mod_rewrite, you can change this into something much easier to read (and remember) for your users:
www.example.com/help
Hence what mod_rewrite does, is rewriting the url in the background. Your user enters www.example.com/help into his browser’s address-bar, but what is delivered is www.example.com/index.php?page=help.
The great thing about mod_rewrite is, that the user is not redirected, i.e. he sees the content from www.example.com/index.php?page=help, but the address-bar still reads www.example.com/help.
Availability and Implementation
Basic mod_rewrite techniques like the above are relatively easy to implement. And access to mod_rewrite is nowadays widely available on many shared hosting services.
There’s more (forbid image hotlinking)
Apart from the above, mod_rewrite offers further interesting possibilities. For example, it can avoid people hotlinking to your images from other websites by delivering a "403 Forbidden"-message instead of the actual image.
Recommended Articles
Read more about mod_rewrite on the following sites (those have helped me, and I recommend reading them in the order as listed, but there are many other articles out there, too):
- mod_rewrite, a beginner’s guide (with examples)
http://www.workingwith.me.uk/articles/scripting/mod_rewrite - Use Apache’s mod_rewrite to make URLs more user friendly
http://articles.techrepublic.com.com/5100-10878_11-5068743.html - Beginning Mod Rewrite
http://www.webmasterworld.com/forum92/4333.htm - Mod Rewrite and Regular Expressions
http://www.webmasterworld.com/forum92/4332.htm - German: Anleitungen Tutorials und Hilfe zum Apache Modul mod_rewrite.
http://www.modrewrite.de/ - Apache Module mod_rewrite (The Module’s Documentation)
http://httpd.apache.org/docs/current/mod/mod_rewrite.html