Howto mod_rewrite with Apache Published: Apr 29, 2004
  • Rating

    4/5

Create search engine friendly URLs with Apache mod_rewrite which rewrites requested URLs on the fly. It supports an unlimited number of rules making it perfect for dynamic web sites.

About mod_rewrite for Apache

This module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly. It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule to provide a really flexible and powerful URL manipulation mechanism. The URL manipulations can depend on various tests, for instance server variables, environment variables, HTTP headers, time stamps and even external database lookups in various formats can be used to achieve a really granular URL matching.

This module operates on the full URLs (including the path-info part) both in per-server context (httpd.conf) and per-directory context (.htaccess) and can even generate query-string parts on result. The rewritten result can lead to internal sub-processing, external request redirection or even to an internal proxy throughput.

But all this functionality and flexibility has its drawback: complexity. So don't expect to understand this entire module in just one day.

Using Apache's mod_rewrite

The Apache module mod_rewrite can be used to perform various forms of URI acrobatic manipulation. A prerequisite concept before attempting to understand mod_rewrite are regular expressions.

When a URL is requested by a server, this does not necessarily map directly to the server's filesystem. This request can be twisted and turned to (hopefully) present more sense to the browsing user.

Why should I care?
 
A clean URL is part of a good user experience. It works as a breadcrumb trail - allowing the user to see where they are located in the site, it doesn't break in bookmarks, you can easily send it over email, and allows users to guess where they want to go next. Most importantly a easy to read URL will be indexed by search engines such as Google. Having all your pages indexed creates a huge advantage to getting visitors to your website. Many search engine robots cannot read a URL with symbols such as ? & or commas and therefor not indexing your website.

This is all possible by using human-readable URLs:

"In principle, users should not need to know about URLs which are a machine-level addressing scheme. In practice, users often go to websites or individual pages through mechanisms that involve exposure to raw URLs." -- Jakob Neilsen, Jakob Nielsen's Alertbox, March 21, 1999: URL as UI

"a URL should contain human-readable directory and file names that reflect the nature of the information space." -- Jakob Nielsen, item #4 Top Ten Mistakes in Web Design

By choosing a well thought-out URL, you won't have to change it during the next re-organization. URLs that remain the same tend to pick up more links over time.

Getting started:
 
First, Apache must be compiled with the mod_rewrite module for any of this to take place. Insert these lines into the vhost definition for the domain that you want to work with.

RewriteEngine on
RewriteLog /path/to/logs/server.rewrite.txt
RewriteLogLevel 1

The first line turns the RewriteEngine on. Otherwise, extra code doesn't get processed by the Apache webserver. Next, we specify where the logfile that records the rewrite activity should be placed. This is mostly for debugging, as your CustomLog should be keeping track of traffic.
 
A beginning example:
 
One of the simplest uses of mod_rewrite is to re-direct a web request from one page to another. Many times this will be done if the first has expired, was spelled wrong, or the site has a new naming scheme. It's nice to forward new users to the correct page in case they have the previous one bookmarked, or if a search engine has cached the old location.

RewriteRule ^/biogarphy.php3 /biography/ [R=301]

This forwards a browser request from one page to the other. because the [R=301] at the end. I've taken a file that was spelled wrong, and fixed it at the same time removing a an old filetype suffix. (php4 has replaced that suffix with .php) What if I were to dump php from my system, and go with *.html, *.jsp, or even *.willie? By rewriting my URI to look like a directory, it doesn't matter what filetype I'm using, nor what my DirectoryIndex options are.

Compound Example:
 
What if you used the above example, but didn't decide to create a "biography" directory at your Doc-root? Apache can still be told where the content resides by including another RewriteRule following the first. Rules will continue attempting to match until a "last" case is presented with the [L] modifier at the end. This is much like a switch programming structure, using break to prevent each option from being executed.

RewriteRule ^/biography/ /biogarphy.php3 [L]

This might seem a little redundant, since we just did the opposite. This line will tell requests to "biography" to read the content from /biogarphy.php3 instead of looking for a biography directory. Confusing? Well, I could do this instead:

RewriteRule ^/(.+)/?$ /content/$1.php [L]

I can search on anything that follows the beginning slash, and replace the file request to look for that file in the content directory through the use of the regular expression and the backreference.

I've also placed this inside another directory that I don't necessarily want the browsing user to see, or know about, but it's easier for the webmaster to keep track of the roles of each file on the site. Since I've upgraded from php3 to php4 the suffix has changed. 
 
More Advanced - the Query String:
 
The query string is passed in separately from the URL. This means that a simple regex doesn't necessarily do the trick, but a compound statement using RewriteCond (condition) is required.

RewriteCond %{QUERY_STRING} id=([^&;]*)
RewriteRule ^/$ http://%{SERVER_NAME}/%1/? [R]
RewriteRule ^/([^\/]*)/?$ /index.php?id=$1 [L]

The RewriteCondition matches only when the following condition is true, and continues until a "last" [L] is stated. The Condition's backreferences are different, using the % prefix, and their scope lasts beyond the Condition line.

This above example would translate "/?id=home" into "/home/", and then re-assign the value of "home" to the id HTTP_GET_VAR. One more thing to notice here is that the the second line has a trailing ? - this is used to negate copying of the query string into the new, re-directed URI.
 
More Reference Links:
 
http://www.engelschall.com/pw/apache/rewriteguide/
http://httpd.apache.org/docs/mod/mod_rewrite.html
http://httpd.apache.org/docs/misc/rewriteguide.html
 

 

  • Rating

    4/5

Related Articles

Comments (24)

  • Gravatar - Boby
    Boby 15:35, July 27, 2006
    Your site is very interesting and useful
  • Gravatar - Ronny
    Ronny 06:13, July 28, 2006
    Wow. This site is great. I have no words to said more than thanx.
  • Gravatar - Genry
    Genry 11:28, July 28, 2006
    This site simply ideal. Success in work!
  • Gravatar - Eric
    Eric 02:31, July 29, 2006
    Very cool design! Useful information. Go on!
  • Gravatar - Nike
    Nike 07:38, July 29, 2006
    Very interesting website. Keep up the outstanding work and thank you...
  • Gravatar - Boby
    Boby 18:56, July 29, 2006
    Hi, all. Nice site...I really like your site ! Good job man.
  • Gravatar - Amanda
    Amanda 02:14, July 30, 2006
    Oh o ho! very nice site!
  • Gravatar - Bill
    Bill 14:42, July 30, 2006
    Cool guestbook, interesting information... Keep it UP. excellent site i really like your stuff.
  • Gravatar - Bill
    Bill 03:45, July 31, 2006
    i love this site.
  • Gravatar - Mike
    Mike 11:28, August 2, 2006
    A fantastic site, and brilliant effort. A great piece of work.
  • Gravatar - Katie
    Katie 01:02, September 26, 2006
    I'm love this great website. Many thanks guyd
  • Gravatar - Joon
    Joon 14:26, October 28, 2006
    Pretty nice site, wants to see much more on it! :)i
  • Gravatar - yashjimmy
    yashjimmy 16:12, July 9, 2010
    Hi, I am working on the rewritemap and rewriterule for my website. actually requirement is like i have one website whose address is http://www.myside1.com now since i am going to retire this website so i need to redirect most of the pages to new website. i want to use map file for this. i have created this mapping file where i want to keep maping like /folder1/page1.html http://newwebsite.com/content/view/hello.html in this way i want to use the redirection. i dont want to hardcode the each redirect rule. I can have /folder2/page1.html also. Could you please help me to have the correct rule.
  • Gravatar - boroPadloro
    boroPadloro 19:01, May 24, 2011
    <a href="http://04velegonines2010.1-2-3host.com/user-id271/21121.html">xbox live account</a> xbox auction
    <a href="http://43xbox360blur2010.hostaim.com/userid6058/2587.html">xbox 360 emulator for vista</a> xbox 360 ethernet port not working
    <a href="http://00xbox480i2010.00sites.net/userid5140/0635.html">xbox 360 storage tower</a> xbox forums banning
    <a href="http://16celiaris2010.14it.net/topic-id148/2262.html">xbox live msn</a> sony ps3 release dates
    <a href="http://19validius2010.i8it.net/blog-id037/5130.html">xbox 360 sales on black friday</a> fantasy final ps3 trailer vii
    <a href=http://03arulelis2010.10-host.com/topic-id255/8048.html>xbox 360 ps3 controller</a> xbox live mlg bio
    <a href=http://58xboxkaiddd-2010.10-host.com/userid1050/0641.html>xbox bio art crown</a> xbox 360 value bundle
    <a href=http://41xbox480pgam2010.007sites.com/userid2659/23119.html>es language ps3 warcraft world</a> xbox live no credit card
    <a href=http://17viscaro2010.14it.net/user-id136/5243.html>xbox component cables</a> xbox unreadable disc fix
    <a href=http://24silia2010.hostevo.com/forum-id385/5373.html>xbox controller on pc driver</a> ps3 blue tooth wireless controller
    http://01xboxgifts2010.hostevo.com/post-id943/index.html xbox 360 ubuntu media center
    http://11trulmius2010.720north.com/post-id478/index.html xbox developers
    http://33xboxpartsli2010.720north.com/userid0825/index.html xbox lg competition
    http://98xbox3dstere2010.00sites.net/userid8850/index.html xbox customer care india
    http://45xbox20gb2010.007sites.com/userid8680/index.html xbox 360 elite modern warfare 2 bundle walmart
    icq685768950486
  • Gravatar - boroPadloro
    boroPadloro 21:31, May 24, 2011
    <a href="http://03arulelis2010.10-host.com/topic-id255/48110.html">xbox country rock band</a> xbox 360 game reviews top
    <a href="http://08xboxforums32010.55fast.com/blog-id668/1969.html">sky player on xbox lags</a> xbox 360 cheap console
    <a href="http://34xboxe32010.14it.net/blog-id977/7197.html">xbox 360 usb hard drive hack</a> xbox 360 repaired or replaced
    <a href="http://14veritiulus2010.1-2-3host.com/forum-id458/507.html">xbox 360 jasper chipset and how to spot it</a> xboxaddict
    <a href="http://76xboxrobotco2010.00sites.net/userid1411/2397.html">xbox bio art tut</a> xbox live your nat type
    <a href=http://42xboxgearsto2010.720north.com/userid4645/04120.html>xbox 360 kodu</a> xbox guy game
    <a href=http://43xbox4702010.hostaim.com/userid2766/4822.html>xbox 360 oblivion cheats codes</a> xbox hotswapping
    <a href=http://31alfonenus2010.812host.com/blog-id968/1582.html>xbox live contact number ireland</a> xbox magazine with disc
    <a href=http://28xbox3702010.hostaim.com/userid0572/2590.html>xbox american idol song list</a> xbox 360 unreadable disc fix
    <a href=http://26xboxgbsuppo2010.10-host.com/blog-id925/8481.html>xbox 360tm universal media remote</a> xbox birthday cards
    http://04veceius2010.812host.com/blog-id490/index.html xbox names ideas
    http://08vectroniulus2010.14it.net/post-id585/index.html flashed xbox 360 for sale
    http://34coponinius2010.812host.com/topic-id233/index.html xbox 360 xtractor
    http://44xboxpulse722010.10-host.com/userid2958/index.html xbox dos attack
    http://20polarius2010.720north.com/post-id900/index.html xbox 360 cases and mods
    icq685768950486
  • Gravatar - boroPadloro
    boroPadloro 22:32, May 25, 2011
    <a href="http://77xboxmodscon2010.steadywebs.com/userid1105/353.html">xboxchile</a> xbox live 30 day trial
    <a href="http://12cato2010.720north.com/blog-id978/14105.html">xbox live finder</a> xbox sky player
    <a href="http://33rummunian2010.hostevo.com/topic-id742/3539.html">ps3 killzone</a> xbox 360 ddr mat
    <a href="http://15traetius2010.14it.net/blog-id872/148.html">red dead redemption xbox 360 console</a> xbox game show
    <a href="http://80xboxremotec2010.007gb.com/userid5952/882.html">xbox 360 sbk</a> xbox 360 bios copy and paste
    <a href=http://49xboxgiveawa2010.1-2-3host.com/userid7456/8539.html>xbox network adapter not working</a> xbox thinks game is dvd
    <a href=http://46xboxkeyboar2010.1-2-3host.com/userid3886/5126.html>xbox live leaderboards modern warfare 2</a> xbox live buy games
    <a href=http://33coctaris2010.14it.net/post-id048/9697.html>xbox live price hike</a> xbox 360 save editors benutzen
    <a href=http://93xboxkameo2010.5webs.net/userid9160/index.html>xbox 360 smack talk</a> xbox iphone music
    <a href=http://00xbox3d2010.5webs.net/userid9463/3797.html>xbox live people search</a> xbox audio adapter cable
    http://18xboxlivefri2010.10-host.com/userid7882/index.html xbox 360 fear 2 walkthrough
    http://00xboxmodsdow2010.55sites.net/userid1752/index.html ps3 lobby
    http://41xbox480pgam2010.007sites.com/userid2659/index.html xbox 360 mods
    http://66xboxplay2010.hostaim.com/userid9962/index.html xbox how to delete a profile
    http://20vinian2010.14it.net/forum-id893/index.html xbox usb hard drive
    icq685768950486
  • Gravatar - boroPadloro
    boroPadloro 01:06, May 26, 2011
    <a href="http://03arulelis2010.10-host.com/topic-id255/7658.html">xbox live gold membership free</a> xboxworld360
    <a href="http://15xboxlive7202010.hostevo.com/userid6127/105.html">xbox 360 slogan</a> xbox 360 elite boxing day
    <a href="http://37syndinian2010.14it.net/topic-id459/4680.html">xbox live games</a> xbox live strict nat fix
    <a href="http://17balurdus2010.007gb.com/topic-id188/20122.html">xbox-scene.com m8</a> xbox 360 hulu
    <a href="http://24silia2010.hostevo.com/forum-id385/28125.html">xbox controller on pc driver</a> xbox.com sign in
    <a href=http://01xboxgifts2010.hostevo.com/post-id943/69103.html>xbox docking station</a> xbox 360 specs compared to ps3 specs
    <a href=http://11trulmius2010.720north.com/post-id478/5098.html>xbox emulation</a> xbox bio copy and paste
    <a href=http://35atollia2010.10-host.com/topic-id492/604.html>xbox 360 hard drive transfer disc with pc</a> xbox crossover cable diagram
    <a href=http://18xboxgutswap2010.001webs.com/user-id253/30123.html>xbox controller mod tutorial</a> final fantasy vii ps3 tech demo
    <a href=http://73xboxpurchas2010.steadywebs.com/userid8117/5246.html>xbox button font</a> xbox dvd player problems
    http://84xboxmodsnet2010.720north.com/userid3225/index.html xbox heatsink
    http://43xbox360blur2010.hostaim.com/userid6058/index.html demolition derby xbox 360
    http://85xboxnzsuppo2010.10fast.net/userid0271/index.html xbox adapter flashing green light
    http://18pusnius2010.hostevo.com/post-id219/index.html xbox 360 hard drive 20gb price
    http://47xbox360buy2010.55sites.net/userid2411/index.html it language magic magic might might ps3
    icq685768950486
  • Gravatar - boroPadloro
    boroPadloro 03:50, May 26, 2011
    <a href="http://67xbox-genera2010.solidwebhost.com/userid1470/61130.html">xbox 360 logo freeze</a> xbox 360 games future shop
    <a href="http://38xbox360bf22010.10fast.net/userid2064/6215.html">xbox 360 hard drive upgrade transfer</a> xbox 360 racing games best
    <a href="http://51xboxnz2010.my3gb.com/userid5933/4143.html">xbox360achievements.org modern warfare 2</a> best xbox kids games
    <a href="http://98xbox3dstere2010.00sites.net/userid8850/312.html">xbox 360 os on pc</a> xbox l360
    <a href="http://19cito2010.hostevo.com/forum-id141/1140.html">ps3 cell chips</a> xbox live stats
    <a href=http://01cerdian2010.i8it.net/topic-id937/6773.html>xbox flashing orange</a> xbox joypad
    <a href=http://92xboxmw2cont2010.720north.com/userid9193/4629.html>xbox 360 bio copy and paste</a> xbox cover art
    <a href=http://28pintientius2010.hostevo.com/topic-id297/91103.html>xbox 360 delete profile</a> xbox 360 game saves transfer
    <a href=http://17viscaro2010.14it.net/user-id136/2675.html>xbox jack size</a> what is a xbox lag switch
    <a href=http://24silia2010.hostevo.com/forum-id385/9093.html>xbox heatsink kit</a> xbox controller pc modern warfare 2
    http://07xbox60achie2010.1gb.bg/userid8326/index.html xbox 360 elite game bundle
    http://04xboxmods3602010.10-host.com/userid3877/index.html xbox 360 nxe
    http://31synctacius2010.720north.com/user-id599/index.html metal ps3 twisted
    http://17atirian2010.812host.com/user-id916/index.html ps3 madden video
    http://19validius2010.i8it.net/blog-id037/index.html xbox 360 xcm black light case
    icq685768950486
  • Gravatar - boroPadloro
    boroPadloro 06:43, May 26, 2011
    <a href="http://04velegonines2010.1-2-3host.com/user-id271/8627.html">xbox 360 rf switch</a> xbox 360 dvi
    <a href="http://45xbox360bcfx2010.i8it.net/userid4590/43116.html">soft modding xbox 360 elite</a> xbox cheap games
    <a href="http://28pintientius2010.hostevo.com/topic-id297/31119.html">xbox adapter flashing green light</a> xbox 360 deals target
    <a href="http://28xbox3702010.hostaim.com/userid0572/7358.html">xbox ireland support number</a> xbox specifications
    <a href="http://19validius2010.i8it.net/blog-id037/3697.html">xbox 360 sales on black friday</a> ps3 news and pics
    <a href=http://08besius2010.1-2-3host.com/user-id804/index.html>xbox 360 elite power supply wattage</a> xboxzone.sk
    <a href=http://12cetus2010.hostevo.com/user-id372/55120.html>xbox keeps shutting off</a> xbox 360 clans
    <a href=http://59xboxnuevo2010.5gbfree.com/userid8755/5493.html>last.fm in australia xbox 360</a> xbox 360 game saves site
    <a href=http://66xboxplay2010.hostaim.com/userid9962/8930.html>xbox 360 bundles costco</a> xbox 360 shooter games
    <a href=http://20cacian2010.812host.com/blog-id785/5616.html>xbox parts sale</a> xbox 360 driving games
    http://61xbox4nappyr2010.i8it.net/userid5069/index.html xbox controller drivers
    http://12autus2010.i8it.net/topic-id737/index.html credit https3a2f2freview trans u
    http://35atollia2010.10-host.com/topic-id492/index.html xbox live mac laptop
    http://15traetius2010.14it.net/blog-id872/index.html xbox 360 games on itouch
    http://76xboxrobotco2010.00sites.net/userid1411/index.html xbox forums call of duty
    icq685768950486
  • Gravatar - camDilmimputt
    camDilmimputt 08:07, June 1, 2011
    free download soft web
    softahbrgiou@opilon.com

Add Your Thoughts

WebHostGear.com is a hosting directory, not a web host.

Copyright © 1998-2018 WebHostGear.com