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)
Nike Free Run…
[...]Yeah and Efe how done most people extent besides marketing was Momoko?[...]…
Coolest blog entry……
[...] may potentially have worn out the deficiency of this points motivated by way of your self on [...]…
Visitor recommendations…
[...]one of our visitors recently recommended the following website[...]……
Awesome website…
[...]the time to read or visit the content or sites we have linked to below the[...]……
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Hello…
I intended to create you one bit of word to finally thank you very much yet again relating to the awesome advice you’ve shared on this website. It is simply particularly generous of people like you to provide easily all that a few people could have su…
Related……
[...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……
Related……
[...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
Superb website…
[...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[...]……
Awesome website…
[...] below are some links to sites that many of us link to merely because we believe they’re seriously worth visiting [...]…
News info…
I was reading the news and I saw this really cool info…
Check this out…
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate, just click the links over[...]……
Read was interesting, stay in touch……
[...]please visit the sites we follow, including this one, as it represents our picks from the web[...]……
Visitor recommendations…
[...]one of our visitors recently recommended the following website[...]……
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Great website…
[...]we like to honor many other internet sites on the web, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out[...]……
Cool sites…
[...]we came across a cool site that you might enjoy. Take a look if you want[...]……
Check this out…
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate, just click the links over[...]……
You should check this out…
[...] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well [...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Online Article……
[...]The information mentioned in the article are some of the best available [...]……
Great information…
This is often fantastic. Another one stare upon on quantity therefore we are impressed. We are fascinated by this kind of clothes. The appreciate your specific tape, and benefits your time inside this. Please keep modifying. They may be in fact good mo…
The best sites……
[...] Here are a few pertinent facts [...]…
Favorite link…
[...] plenty of time to read or visit the actual content material or sites we’ve linked to below the [...]…
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Awesome website…
[...]the time to read or visit the content or sites we have linked to below the[...]……
Blogs ou should be reading…
[...]Here is a Great Blog You Might Find Interesting that we Encourage You[...]……
Websites you should visit…
[...]below you’ll find the link to some sites that we think you should visit[...]……
Related……
[...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……
Sources…
[...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]……
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate, just click the links over[...]……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
I {and|and also|as well as|in addition to|not to mention|together with} {my buddies|my friends|my guys|my pals} {appeared to be|came|ended up|happened to be|have already been|have been|were|were actually|were found to be} {analyzing|checking|checking…
came up with|developed|got|I got|I had} {a horrible|a terrible|an awful} {feeling|suspicion} {I had not|I never} {expressed respect to|thanked} {the blog owner|the site owner|the web blog owner|the web site owner|the website owner|you} for {them|those …
Links…
[...]Sites of interest we have a link to[...]……
This site won’t display properly on my apple iphone – you might want to try and repair that…
I have discovered some significant things through your site post. One other stuff I would like to convey is that there are several games in the marketplace designed particularly for preschool age kids. They include pattern identification, colors, dogs,…
Websites worth visiting…
I enjoyed reading your article, many thanks….
What a great Blog…
This is an amazing Site…
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Great website…
[...]we like to honor many other internet sites on the web, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out[...]……
Hello…
I as well as my buddies happened to be checking out the nice advice on your web page and all of a sudden I had an awful feeling I had not thanked the website owner for those techniques. Most of the young men had been for that reason stimulated to learn…
Check this out…
[...] that is the end of this article. Here you’ll find some sites that we think you’ll appreciate, just click the links over[...]……
[...]The information mentioned in the article are some of the best available [...]……
[...]below you’ll find the link to some sites that we think you should visit[...]……
Gems form the internet…
[...]very few websites that happen to be detailed below, from our point of view are undoubtedly well worth checking out[...]……
Links…
[...]Sites of interest we have a link to[...]……
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
Sites we Like……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
Yahoo results…
While browsing Yahoo I discovered this page in the results and I didn’t think it fit…