There comes a time where you feel like shoving your head in a car door and slamming it repeatedly, this is that time. Mod_Rewrite made easy, as easy as it gets I guess, it’s like saying “Learn Quantum Physics in 24 Hours”. Well maybe that is not a fair comparison, either way if you are not familiar with regex and/or you are trying to implement mod_rewrite into an existing site you may need to set plenty of time aside to get it all working correctly.
This tutorial is tailored around WAMP for those looking for an easy way to get started shortening those url’s in preparation for that day you upload to the web. This tutorial will break down in a few simple steps how to enable mod_rewrite, how to write your first few basic rules, and some of the common pitfalls.

Enabling Mod_Rewrite
First things first, lets get mod_rewrite enabled in WAMP. Click on the WAMP icon in your system tray to bring up the menu, scroll to Apache->Apache Modules and look for rewrite_module. It should have a check box next to it, if not click on it to activate it.
Manually Activate Mod_Rewrite:
You can also manually turn this on as well by editing your httpd.conf file within WAMP. You can find this file under bin/apache/conf/ most of the time. When editing the file you will need to find the line that says: “LoadModule rewrite_module” and remove the hash mark from the front of the line if it exists.
Create Your .htaccess File
Now you will need to create your .htaccess file and place it in the root directory for your site. I recommend using notepad or a similar text editor to do this as many editing programs tend to add an extension to the file.
Inside your .htaccess file we need to set a few options before we can effectively use mod_rewrite:
Options +Indexes
RewriteEngine On
RewriteBase /
Once we set these base options we may now start writing a few simple rules. Lets take a look at the rule structure and see how mod_rewrite actually shortens the URLs. The basic format for a rule is:
The above shows a few things, first we say “RewriteRule” to declare a new rule to be used, our “NEWPATH” is what we want the url too look like when we are finished and our “ORIGINALPATH” is what the current URL looks like. Lets do a very basic rewrite so you can see what I mean. Lets say we had a url which was: http://localhost/index.php?category=tools and we wanted it to look like the following: http://localhost/category/tools . We would rewrite the statement as follows:
This will do the following things:
- ^ This symbol is kind of like a wildcard that takes whatever precedes it.
- category/tools is the url we will redirect to.
- index.php?category=tools is the source url that we are rewriting.
So in short we turn that long url into a more friendly one by creating this simple rule. You may be thinking that this will be a pain to write a rule for every category especially if you have 100’s of them. We can throw in some regex and make this rule apply for all categories very easily. Before we do so I would like to explain something, mod_rewrite can take the variables passed through your index and rewrite them in a very easy way. Lets sat your link was to index.php?category=tools&id=722 , it is possible to rewrite both the category and id variables. The first variable category could be written category=$1 and the second variable id could be written id=$2. Now whatever gets passed into $1 or $2 can be evaluated by mod_rewrite.
An example would be:
So with the above section the url is now a lot more fluid. How so you ask? So the $1 becomes the first replacement variable in our rewrite rule or in this case the (.*) which means basically anything can go here. So if we pass tools to this the output would be category/tools. Then the next variable $2 becomes the second part of our statement ([0-9]+) which will allow for any number sequence to go here. So all together this means if we pass index.php?category=tools&id=797 then our rewrite would allow the url to be category/tools/797.
A couple of things to remember:
- Always place your most complicated rules first.
- Do your research on regex, it can save you a lot of time.
- If your index.php or whatever file you are using is not in localhost but a directory of that instead you will have to make sure that your rules state the correct path. example: Site is localhost/mysite/ then in your rewrite rule you will need to specify mysite/index.php
A great tool to help you along:
There is a great cheat sheet for mod_rewrite, you can find it at addedbytes.com
Disclamer, blah blah:
This is a VERY basic tutorial and is meant to get you on the right path when setting up mod_rewrite on your local WAMP server. There are plenty of resources to get you deeper into writing rules as well as a lot of other features that this module is capable of. It is an invaluable tool and should be used as it can redirect, create great blacklists, prevent linking and much more.
Good Luck!
![[del.icio.us]](http://scriptperfect.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://scriptperfect.com/wp-content/plugins/bookmarkify/digg.png)
![[dzone]](http://scriptperfect.com/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://scriptperfect.com/wp-content/plugins/bookmarkify/facebook.png)
![[Furl]](http://scriptperfect.com/wp-content/plugins/bookmarkify/furl.png)
![[Google]](http://scriptperfect.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://scriptperfect.com/wp-content/plugins/bookmarkify/linkedin.png)
![[MySpace]](http://scriptperfect.com/wp-content/plugins/bookmarkify/myspace.png)
![[Newsvine]](http://scriptperfect.com/wp-content/plugins/bookmarkify/newsvine.png)
![[Propeller]](http://scriptperfect.com/wp-content/plugins/bookmarkify/propeller.png)
![[Reddit]](http://scriptperfect.com/wp-content/plugins/bookmarkify/reddit.png)
![[Slashdot]](http://scriptperfect.com/wp-content/plugins/bookmarkify/slashdot.png)
![[Spurl]](http://scriptperfect.com/wp-content/plugins/bookmarkify/spurl.png)
![[StumbleUpon]](http://scriptperfect.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://scriptperfect.com/wp-content/plugins/bookmarkify/technorati.png)
![[Twitter]](http://scriptperfect.com/wp-content/plugins/bookmarkify/twitter.png)
![[Email]](http://scriptperfect.com/wp-content/plugins/bookmarkify/email.png)
Nice post! It definitely helped.
Learning About Welding and due diligence, | Best and Free tips on ……
Welding is the eternal process of joining metals. Welding materials are necessary for a better Metal Works. Environment is a concern for us all today. One….