The Netlobo logo - a Nevada desert landscape

Clean URLs using Apache's mod_rewrite

An example of how to clean up your URLs using mod_rewrite and Apache

Published Aug 11, 2005 by lobo235

One of the issues seen with dynamic websites is the use of URLs that include a very long query string. For example, you might see a URL like the following on a dynamic website:

http://www.netlobo.com/index.php?show=article&id=32&lang=en

A URL like the one above can confuse some search engines and it just doesn't look clean. Now, consider this next URL and compare it to the first:

http://www.netlobo.com/article_32_en.html

As you can see, it's much shorter and cleaner looking than the first. In this article I will show you how you can clean up your URLs using the mod_rewrite module in Apache.

Apache's mod_rewrite

The mod_rewrite module does exactly what it's name implies, it rewrites. A webmaster can use it to rewrite URLs using certain conditions and expressions. Using this module, you can make it so users see a clean URL which appears static but still have the page be dynamically generated. The mod_rewrite module is used by placing a few lines in your .htaccess file or your Apache config file. The first thing you need to do is turn on the Rewrite Engine and set the RewriteBase using the following lines in your .htaccess file:

RewriteEngine on
RewriteBase /

After you turn the Rewrite Engine on then you can setup rules in your .htaccess that the engine will use to actually rewrite the URLs. So, let use the second URL again. As far as I can tell, sometimes you need the RewriteBase / line and other times you don't, depending on the server/host you use. The page being requested is article_32_en.html. We have separated the different parts of the page name by using the '_' (underscore) character. What we want to do is take each part of the page name and feed it into the first URL so it gets passed to our index.php script. To do this, we need to setup a rewrite rule. See the example below that you will use as the second line in your .htaccess file:

RewriteRule ^(.*)_(.*)_(.*).html index.php?show=$1&id=$2&lang=$3

This may look a bit cryptic at first but hopefully I can explain it to you in a way that makes sense. The ^ character says to start at the beginning of the page name being requested. Then each (.*) says match zero or more characters. Then we have our '_' (underscores) that separate everything followed by our extension, .html. The we have the Page where everything will be rewritten to. The $1 matches the first word of the page name, the $2 matches the id, and finally, the $3 matches the language. So, what we end up with is a rewrite that changes the clean url into the functional one that we need for our script to work. The user only sees the clean URL .

I hope this information helps you with mod_rewrite by providing a simple example you can mimic in your own .htaccess file. Please let me know if you have any feedback regarding this article.

0 comments for this article.

del.icio.us logo add this article to del.icio.us!
Other great Web Development and Programming articles on Netlobo.com:
Double Form Submit Problem
PHP Application Feature Management
Preventing Image Hotlinking using mod_rewrite