Mod_Rewrite Tutorial

The .htaccess file has many uses but one of the most popular is turning dynamic URLs into static URLs. In other words, URLs that typically have several parameters can be made to appear shorter and simpler. Search engines are much more likely to follow, index and cache a static html link as opposed to a dynamic link. Other uses of .htaccess files include password protecting sites, custom 404 pages and redirects. However, for this tutorial, I’ll be focusing on the aspect of turning dynamic URL’s into static URL’s often referred to as mod_rewrite.

This tutorial assumes that you are using a Windows PC to create your .htaccess file and your web host is running the Apache web server. Almost all Linux or Unix based web hosts use Apache.

To give you an example of what we are trying to accomplish take the following URL as an example:

http://www.example.com/catalog.php?cat=books&id=1434

Using mod_rewrite we can cause the URL to appear as this:

http://www.example.com/books/1434.htm

Not only will the new URL appear in the browser for visitors but also this is the URL that search engine spiders will use to crawl and index. So what’s happening here? The .htaccess file contains certain instructions that tell the web server to go get the /catalog.php?cat=books&id=1434 page when ever a request is made for the /books/1434.htm page and present it to the user. This is the power of .htaccess and mod_rewrite. You can turn hundreds or even thousands of URLs into static, easy to remember, search engine friendly URLs.

So let’s get started. Open up notepad and then cut and paste the following text:

Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^books/(.*).htm$ catalog.php?=cat=books&id=$1 [L]

Save the Notepad document.

Go to the File menu.
Choose Save.
In the Save in field, save the file to your desktop or somewhere convenient on your hard drive.
In the File name box, type .htaccess. (Don’t forget the “.” in front.)
In the Save as type box, use: All Files.
In the Encoding box, use: ANSI.
Click Save.
Close the Notepad document (which is your .htaccess file).

If Windows gives you any trouble saving the file as .htaccess, you may have to name it htaccess.txt and then change the name in the next step.

Once the file is saved upload it to your web host in the same place where your index page resides. For this example nothing is going to happen unless you have dynamic pages that match the example exactly. You’ll have to modify the example to get it to work for your site. This tutorial is only meant to give you an idea of the power of mod_rewrite and how to use it.

Let’s break down each line to try and get a better understanding of how this works.

  1. Options +Indexes – The Apache guide says that this causes any directory without an index page to return a list of all the files in contains. I also found that sometime mod_rewrite doesn’t work without it. Not all web hosts require that this parameter be listed by I’ve found it’s easier just to include it by default because it won’t hurt anything.
  2. RewriteEngine on – Turns on mod_rewrite
  3. RewriteBase / – Uses the root directory as the starting point for all URL’s in the .htaccess file.
  4. RewriteRule ^books/(.*).htm$ catalog.php?=cat=books&id=$1 [L] – This is really were the work is being done. Let’s break it down even further:

a. RewriteRule – Says that a mod_rewrite instructions are about to follow

b. ^books/(.*).htm$ – Mod_rewrite is looking for a URL following the root directory of your domain starting with the word books and then followed by any string of characters and ending in .htm. Here are some more examples:
www.example.com/books/moby-dick.htm <- Qualifies for this rule
www.example.com/books/1234.php <- Doesn’t qualify because it ends in php.
www.example.com/books/books/andmorebooks.htm <- Nope, doesn’t follow the pattern.
www.example.com/books/2342jhasdfhi1234.htm <- Yes

See, (.*) is a wild card and it means any combination of numbers, letters or dashes will be put into a variable called $1.

c. catalog.php?=cat=books&id=$1 [L] – And the last part. This is what tells mod_rewrite to tell the web server what page to really fetch and return to the user. The $1 is replaced by whatever was found in (.*).

And that’s it! Keep in mind that from your homepage (and other pages in your site) you will link to a static URL but when a visitor or search engine spider follows the link, the web server will turn that request into the dynamic URL that actually exist on your server. The static link is fictitious. The dynamic URL is the page that really exists.

Mod Rewrite Tutorial by Searchspammer