Rewriting URLs with mod_rewrite

October 3, 2010

web_devlogo_online 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):

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.