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)
Websites we think you should visit…
[...]although websites we backlink to below are considerably not related to ours, we feel they are actually worth a go through, so have a look[...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Trackback for this URI…
[...] we really like to remember many other internet sites on the internet, regardless of whether they are not linked to us, simply by linking to them. Down below are a couple of sites truly worth visiting [...]…
Websites worth visiting…
[...] Blogpost: article .. discovered this post with regards to [...]…
filmiki erotyczne…
Super cipka w filmie erotycznym, warto to zobaczyc….
Extra Reading…
[...]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[...]…
Websites we think you should visit…
[...]although websites we backlink to below are considerably not related to ours, we feel they are actually worth a go through, so have a look[...]……
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Sources…
[...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
Websites we think you should visit…
[...]although websites we backlink to below are considerably not related to ours, we feel they are actually worth a go through, so have a look[...]……
Hello…
Found your weblog and decided to have a very quick study, not what I usually do, but this blog is wonderful. Great to see a Blog for a change that isn?t full of spam and rubbish, and understandable. Awesome blog…
Online Article……
[...]The information mentioned in the article are some of the best available [...]……
Wow!…
A very awesome post….
………..
I suppose for the time being i am going to accept book-marking as well as incorporating the RSS feed for you to our Yahoo and google account. My partner and i anticipate fresh updates and may share this website along with my personal Facebook or myspac…
to be a prime contributor to the…
deregulation of normal function experienced by not only women approaching, transitioning, and resolving the challenge of menopause, but also young women experiencing difficulty when fertility begins also referred to as menarche.the imbalance is initial…
Hello…
Needed to put you the little observation in order to give many thanks as before on the pretty guidelines you have provided at this time. This has been strangely generous with you to give publicly exactly what many of us might have offered for sale as a…
Its hard to find good help…
I am regularly proclaiming that its hard to find quality help, but here is…
Favorite webpage…
[...] the best time to study or visit the particular written content or websites we have linked to below the [...]…
Awesome site…
[...] Pretty much every now and then many of us go with blogs that we read. Down the page are the hottest sites that we like [...]…
Coolest news on earth, you have to see this!…
[...] And here is some relevant facts [...]…
Sites we Like……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
[...]the time to read or visit the content or sites we have linked to below the[...]……
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
I much like it!…
Incredible post. I want you up-to-date your weblog a great deal much more usually, I just can’t appear to be to own adequate of one’s weblog. I saved your weblog in my bookmarks. Would it be feasible to do a guest post sometime?…
You should check this out…
[...] Many males and females have total-time characteristic and are gonna be undertaking a profession swap [...]…
The best sites……
[...] Now each time an individual looks at the current fashion assertion, it is usually measurement zero [...]…
Great information…
This is certainly first-class. Only checked out associated with peace of mind so we are surprised. We are precisely fascinated by this type of activities. Experts appreciate household tips, and value for money your time inside this. Please keep cutting…
is placed near to the ear drum…
it gives a more clear and natural amplification of sounds.7. open-fit devices designed to mitigate the occlusion effect in other devices, oft is similar to the bte. it has a small case sitting outside the ear and a tube running into…
… [Trackback]…
[...] Here you can find 66008 additional Informations: scriptperfect.com/2009/09/mod_rewrite-tutorial-for-wamp-modrewrite-mod-rewrite/ [...]…
Blogs ou should be reading…
[...]Here is a Great Blog You Might Find Interesting that we Encourage You[...]……
Here is something I found out today:…
I recommend you check out this awesome article…
copic sketch markers cheap…
[...]the time to read or visit the content or sites we have linked to below the[...]…
they want to be approached by writers….
some only want agented material, so forget those, unless you want to take the time to try and get an agent. (there are writers digest books geared to this, as well). other publishers will only accept “exclusive” manuscript submissions. this means…