Script Perfect

         Random snips of code and bugs

Mod_Rewrite Tutorial for WAMP (modrewrite mod rewrite)

Posted by Tim On September - 3 - 2009

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 +FollowSymLinks

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:

RewriteRule NEWPATH ORIGINALPATH

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:

RewriteRule ^category/tools$ index.php?category=tools

This will do the following things:

  1. ^ This symbol is kind of like a wildcard that takes whatever precedes it.
  2. category/tools is the url we will redirect to.
  3. 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:

RewriteRule ^category/ (.*)/([0-9]+)$ index.php?category=$1&id=$2

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:

  1. Always place your most complicated rules first.
  2. Do your research on regex, it can save you a lot of time.
  3. 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!

2,384 Responses to “Mod_Rewrite Tutorial for WAMP (modrewrite mod rewrite)”

  1. Amber says:

    Nice post! It definitely helped.

  2. 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….

  3. Moshe says:

    Thanks – just what I needed!

  4. Thank you……

    Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up!…

  5. Thank you……

    I truly loved this page, appreciate sharing and also I have added your website for upcoming use….

  6. Rob says:

    I did write a little more in depth tutorial on my blog at http://www.web-design-talk.co.uk/116/seo-friendly-urls-with-mod-rewrite/

    When your talking about paths in your htaccess file have a read up on RewriteBase – it will save you a lot of hassle :)

  7. Good! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?…

    Hello, this is my first time i visit here. I found so many interesting in your blog especially on how to determine the topic. keep up the good work….

  8. I’ve just started off a blog, the knowledge you give on this site has aided me extremely. Thank you for all your time & work….

    Of course, what a great site and informative posts, I will add backlink – bookmark this site? Regards, Reader…

  9. wulijun says:

    普拉提减肥 pilates.T恤衫T-shirt,文化衫T-shirt.新加坡投资移民Singapore Investment Immigration.投资移民investment immigration.澳洲投资移民Australia investment immigration.香港投资移民Hong Kong investment immigration.加拿大投资移民canada Investment Immigration.雷达测速仪 Dynamometer仪器酒精检测仪Alcohol Tester.酒精测试仪Alcohol Tester.电机测试系统 Dynamometer,测功机 Dynamometer.氯醋树脂VINISOL OH.城市综合体城市综合建筑新天地集团.餐桌椅Dining,快餐桌椅空气过滤器Air Filter,油水分离器Oil-water separator,工业冷水机Industrial Chiller,实验室家具Laboratory furniture.

  10. OpenOffice Download…

    [...Be Sure You Accept This Trading Links Request...] …

  11. OpenOffice Download…

    [...Kindly Accept This Link Exchange Invite...] …

  12. Kasper Suits…

    [...Make Sure to Accept This Swapping Invitation...] …

  13. Kasper Suits…

    [...exciting and I am going to put it to use on my personal web site, thanks a lot..]…

  14. Kasper Suits…

    [...somewhat and I'm planning to make use of it at my web site, thanks for the tips..]…

  15. OpenOffice Download…

    [...Kindly Accept This Link Exchange Invitation...]…

  16. I opine that to get the business loans from banks you should have a firm reason. But, one time I’ve got a auto loan, just because I wanted to buy a building.

  17. Prevention of Diabetes…

    [...]symptoms of hypoglycemia include shakiness[...]…

  18. online marketing summit 2011…

    [...]Earning second income is usually an outstanding experience. There are many opportunities out there available to reach your goals at it that you may possibly assume this[...]…

  19. Dark Circles Under Eyes…

    [...]new studies show eye circles are a problem of the p[...]…

  20. iPhone eBook Reader…

    [...]your Apple iPhone can also display eBooks in various formats[...]…

  21. Jtosme Movie says:

    Tks…

    I’ve visited many blogs,forums, but this time:How your fantastic forum is . It makes me surprise….

  22. Diabetes…

    [...]a look at the top reasons behind[...]…

  23. Tks…

    I’ve visited many blogs,forums, but this time:How your fantastic forum is . It makes me surprise….

  24. Bedroom sets…

    [...]here are some links to sites that we link to because we think they are worth visiting[...]…

  25. white bedroom furniture…

    Sweet blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thanks…

  26. nordstrom dresses…

    Hey there, I think your website might be having browser compatibility issues. When I look at your website in Safari, 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 th…

  27. What Causes Dark Under Eye Circles…

    [...]a look at the top reasons behind[...]…

  28. Prevention of Diabetes…

    [...]symptoms of hypoglycemia include shakiness[...]…

  29. Dark Under Eye Circle…

    [...]removed in just 30 days of free treatment[...]…

  30. Debt Relief…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  31. Паради says:

    Пруфлинк бы не мешал……

    Не хотите рекламой обменяться?…

  32. nordstrom dresses…

    I was wondering if you ever thought of changing the page layout of your website? 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…

  33. Hi…

    Great site you got here. Good job with the post as well. I found this related post that you may want to check out….

  34. Title…

    I just added this website to my rss reader, excellent stuff. Cannot get enough!…

  35. Another Title…

    I think other site owners should take this website as a model, very clean and excellent user genial style and design, let alone the content. You are an expert in this topic!…

  36. kosmetyki…

    I was wondering if you ever thought of changing the layout of your site? 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…

  37. noni…

    I’m really enjoying the design and layout of your site. It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a developer to create your theme? Exceptional work!…

  38. Websites you should visit…

    [...]below you’ll find the link to some sites that we think you should visit[...]……

  39. How To Get Abs…

    I was curious if you ever considered changing the structure of your website? 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 …

  40. Useful Links…

    I saw this really good post today, I would like to share it with you all….

  41. Another Title…

    I saw this really great post today….

  42. Police Car Auctions…

    This design is spectacular! You most 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!) Fantastic job. I really enjoyed what you had to say, and more than th…

  43. Perry Rose says:

    It’s interesting that you think this way. I’m going to review some more of your posts, but so far I’m liking what i’m reading! Filter Shekan

  44. Eugenie says:

    really…

    Fat women and girls always worried because of their ugly shaped bodies. They want to wear expensive and stylish outfits . http://sdkjuyt.blox.pl/html but they can not wear these outwears…

  45. Dehmer says:

    really…

    Fat women and girls always worried because of their ugly shaped bodies. They want to wear expensive and stylish outfits . http://www.wallinside.com/login-ffgggder.html but they can not wear these outwears…

  46. Pterker says:

    Greate…

    It’s such a great site! http://mylobea.sweetcircles.com/2011/06/24/british-bonanza-for-wimbledon-fans/ Great post, I just bookmarked it on Digg….

  47. Useful link…

    Hello Everyone I just came across this very useful page, you may want to visit it sometime….

  48. registry cleaners…

    This design is incredible! You most 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!) Great job. I really loved what you had to say, and more than that, how…

  49. Cat Training says:

    Related……

    [...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……

  50. Dchelle says:

    hello…

    visit it, http://eugenia.speedywap.net/ , thanks again….

Leave a Reply

Spam protection by WP Captcha-Free

About Me

I am an independent web developer and webmaster of many sites. The main goal of Script Perfect is to provide answers to some of the hard to find questions when it comes to website design and coding.

Twitter