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)
hello…
visit it, http://smartes.bricoblog.fr/ , thanks again….
very helpful…
I preferred to thank you for this good article. http://qmdxb.insanejournal.com/ I by all odds liked every little bit of it…
hello…
visit it, http://sylviaes.spyuser.com/ , thanks again….
Online Article……
[...]The information mentioned in the article are some of the best available [...]……
hello…
visit it, http://nathan.bloginthedark.com/ , thanks again….
efile tax return…
[...]The eFile Tax Return is a simple and convenient way to pay your taxes online. In fact, it’s so quick and easy, you can even do it yourself. [...]…
Online Article……
[...]The specifics mentioned inside the write-up are a few of the best obtainable [...]……
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 [...]……
Recommeneded websites…
[...]Here are some of the sites we recommend for our visitors[...]……
The incestuous relationship between government and big business thrives in the dark….
Hello there, just became alert to your blog through Google, and found that it’s truly informative. I am gonna watch out for brussels. I’ll appreciate if you continue this in future. Lots of people will be benefited from your writing. Cheers!…
Going to church doesn’t make you a Christian any more than standing in a garage makes you a car….
We live in a society where pizza gets to your house before the police….
You blog is so lovely that speak the words right out my month. . I bookmarkt you so that we can talk about it in details, I really can’t help myself but have to leave a comment,you are so good.
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
Police Car Auctions…
Hi, I think your website might be having browser compatibility issues. When I look at your website in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, ver…
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 [...]……
Sites we Like……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
My Trackback…
[…]If you are supposed to learn from your mistakes, why do some people have more than one child.[…]…
Regarding your post…
Someone said the actual submit and totally accept A person. The talk about is excellent and I will discuss that having my friends in addition to facebook contact lenses. Only has a web site with your specialized niche I would will give you a link excha…
Regarding your post…
I read the submit as well as totally trust A person. Your discuss is excellent i will certainly discuss the idea together with my friends in addition to myspace associates. If i has a web page inside your market I might will give you link exchange. One…
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[...]……
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[...]……
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[...]……
Butler Auto Auction…
Hi there! This is kind of off topic but I need some guidance from an established blog. Is it tough to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about creating my own but I’m not sure where to…
This is my Excerpt…
I saw this really great post today….
This…
I saw this really great post today….
solar power electricity…
[...]diabetes could lead to kidney failure, heart disease, blindness, and research had shown[...]…
Your article grabbed my attention…
I read A person’s report and will have to admit the best way amazing it can be. Not only niche wait, how focused you are in sharing that. I most certainly will undoubtedly write about the item utilizing my buddies and definately will search for them. …
Your article grabbed my attention…
Someone said your write-up as well as will need to tell you how awesome it’s. Not only subject matter wait, how focussed experts discussing this. Let me absolutely promote it all using my pals and definately will search for the item. I am going to joi…
Visitor recommendations…
[...]one of our visitors recently recommended the following website[...]……
Title…
This is my Excerpt…
Links for the day!…
Good day everyone. I Just Saw this good site i would like to share it with you guys….
British TV Abroad…
I found your weblog website on google and examine a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN Information Reader. Searching for ahead to reading more from you in a while!……
Recent Blogroll Additions……
[...]usually posts some very interesting stuff like this. If you’re new to this site[...]……
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[...]……
Sites we Like……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
Action is the real measure of intelligence….
I like the helpful info you provide in your articles. I’ll bookmark your blog and check again here frequently. I’m quite sure I’ll learn lots of new stuff right here! Best of luck for the next!…
Sources…
[...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]……
Read was interesting, stay in touch……
[...]please visit the sites we follow, including this one, as it represents our picks from the web[...]……
I am certainly not one of those who need to be prodded. In fact, if anything, I am the prod….
We’re a group of volunteers and starting a new scheme in our community. Your website offered us with valuable information to work on. You have done an impressive job and our whole community will be thankful to you….
Awesome website…
[...]the time to read or visit the content or sites we have linked to below the[...]……
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[...]……
Useful Links…
I saw this really great Page today, I would like to share it with you all….
Websites you must visit…
[... ]below you’ll have the link to some sites that people think you should visit[... ]……
Visitor recommendations…
[...]one of our visitors recently recommended the following website[...]……
ADHD in children…
[...]outcome of a debate may be decided by audience vote[...]…
Szkoly Angielskiego…
I was curious if you ever thought of changing the layout of your blog? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text f…
Sites we Like……
[...] Every once in a while we choose blogs that we read. Listed below are the latest sites that we choose [...]……
Superb website…
[...]always a big fan of linking to bloggers that I love but don’t get a lot of link love from[...]……
Lose Belly Fat…
This design is steller! You certainly know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Wonderful job. I really loved what you had to say, and more than that, how you…