MOD_REWRITE is a module for the Apache Web Server that assists in cleaning up messy-looking URLs like this one:

http://jstiles.com/index.php?area=Blog&page=My latest blog posts 

...and replacing them with more user-friendly URLS like this one:

http://jstiles.com/Blog/My latest blog posts

Advantages of "Friendly" URLs

The following are just a few reasons why you should use MOD_REWRITE and/or make the URLs to your website more user-friendly.  

Friendly URLS Are Easier For Search Engines to Index

This is probably the biggest reason.  Some search engines won't index messy URLs which can negatively impact your search engine ranking. Luckily, the big search engines like Bing and Google have no problem indexing messy URLs, but that doesn't mean you shouldn't bother.  There are other advantages; read on!

Visitors Like URLS They Can Understand and Read

It may even nudge them to bookmark a URL.  Also, it's WAY easier to remember a friendly URL over a messy one.

Better For Security and Maybe Even Your Peace of Mind

Friendly URLs mask the parameters that are actually used to display a page on a website.  This makes it harder for hackers to access your website with a front door attack.  Also, it masks filenames which can give hackers an idea of how your website operates.

How To Use MOD_REWRITE

Here are the steps to take to enable MOD_REWRITE and begin using it to make your URLs more user-friendly:

1. Let Apache Know You Want To Use MOD_REWRITE

Edit your httpd.conf (apache configuration file) and look for "rewrite_module" or "mod_rewrite"; uncomment it (take out the leading # character).

2. Create a .htaccess File

Create a .htaccess file in the directory you want to use rewrite rules in.  In the .htaccess file, put the following on a new line:

RewriteEngine on

This lets Apache know you will be using the rewrite engine in the directory the .htaccess file resides in.

3. Create the rewrite rules.  

First, you need to understand the syntax for a rewrite rule:

RewriteRule  [pattern] [substitution]

[pattern] is where you will input a regular expression and is where you will input the actual address (the messy URL!) that your server will forward the request to.  

For example, my website structures content by category (area) and page.  A request to the "Blog" category and the "My latest blog posts" page would look like this (messy url): 

http://jstiles.com/index.php?area=Blog&page=My latest blog posts

The parameters here are "area" and "page" and their values are "Blog" and "My latest blog posts" respectively.  I want my visitors to be able to access the URL above in a more user-friendly way, like this:

http://jstiles.com/Blog/My latest blog posts

Here's my rewrite rule to accomplish this:

RewriteRule ^([^/.]+)/?([^/.]+)$ index.php?area=$1&page=$2 [L]

Let me break this down for you.

^ - means to match the beginning of the query string (so we'll be matching the category/area first)

([^/.]+) - means to match one or more characters that aren't a forward slash or a period and place the result into a variable: $1.  There are two of these that occur in the expression.  The first one will be placed into a variable: $1 and the second into $2.  If I had a third, it would be placed in $3.  Anything enclosed in parentheses returns a variable of which can be used in the substitution portion of the RewriteRule.

/? - means to look for a forward slash.  Since the category/area title will be followed by a forward slash, I've placed this after my first set of parentheses.

$ - is the closing of the pattern

In the [substitution] portion of the rule, you can see that I used $1 and $2 to substitute the matches for the area and page titles respectively. The [L] on the end means that if this rule makes a match then all RewriteRules following will be ignored.  This means that I could have RewriteRules above the one I've specified above to capture other URL forms.

4. Save The File And Test It Out!

Once you've got your rule(s) in place, save the .htaccess file and load up a page to see if it works.  To test mine, I would navigate to http://jstiles.com/Blog/My latest blog posts to see if the page comes up or not.  If it didn't, clearly I have something wrong in my rule; you may need to do some trial and error to get it right depending on your setup.  Practice makes perfect!

MOD_REWRITE is Powerful

Use it for good, not evil.  Good luck! For more information on MOD_REWRITE check out MOD_REWRITE on Apache.org.