Skip Navigation

mod_rewrite Cheat Sheet

 
The mod_rewrite cheat sheet is designed to act as a reminder and reference sheet, listing useful information about mod_rewrite. It includes a list of flags for the RewriteRule and RewriteCond directives, list of server variables, a regular expression guide and several examples of common rules. A description of what is on the cheat sheet follows, or if you are impatient, you can go straight to the full size mod_rewrite cheat sheet:

Regular Expression Syntax


Thumbnail highlighting regular expression syntax.


URL Rewriting with mod_rewrite depends a great deal upon a solid knowledge and understanding of regular expressions. The "RewriteRule" uses regular expressions in patterns extensively. This guide outlines the basic syntax of regular expression.

Redirection Header Codes


Thumbnail highlighting redirection header codes.


When redirecting users with mod_rewrite, sometimes a status code must be sent back to the user. This status code is interpreted by the browser - sometimes it will mean the user is sent on to a new page, sometimes that they are shown a message. This section lists the most commonly used status codes with their meanings.

RewriteRule Flags


Thumbnail highlighting rewrite rule flags.


Rewrite Rules in mod_rewrite can have flags appended to them, indicating to the server how the action described by the rule should be taken. For example, the 'L' flag tells the server not to process any more rules if the current rule is used. This section lists the various RewriteRule flags.

RewriteCond Flags


Thumbnail highlighting pseudo selectors list.


Conditions in mod_rewrite can also have flags appended to them, and again these tell the server how to interpret the current condition. The "NC" flag indicates the condition is case-insensitive, and an "OR" flag between conditions tells the server that the following rule should be applied if any of the preceding conditions are true. The default in mod_rewrite is that, if a rule is preceded by a list of conditions, it should only be applied if all conditions are met. This section lists the RewriteCond flags.

Example Rules


Thumbnail highlighting example rules.


This section lists a few of the most situations mod_rewrite is used for, along with a rule to achieve the desired affect.

Server Variables


Thumbnail highlighting server variables.


Sometimes it is necessary to rewrite URLs based upon variables like the referring URL or the browser being used. These variables can be used in mod_rewrite (including in the RewriteRule or RewriteCond sections), and this section lists the variables available.

Directives


Thumbnail highlighting directives.


mod_rewrite gives the server a series of instructions, called directives. For example, "RewriteEngine On" tells Apache that RewriteRules may be used and to interpret them if found. This section simply lists the directives used by mod_rewrite.

So now that you know what it does, please feel free to print out the mod_rewrite cheat sheet:

Please note: If you wish to link to the mod_rewrite cheat sheet from elsewhere, please link to this page so others find the description, rather than linking directly to the sheet.

And finally, if you like the cheat sheets, and want to say thanks, I do have an Amazon Wishlist. Thankyou very much to those who have already hunted it down and sent me something nice - I'm very grateful!
 

Syndication

If you like this post, subscribe to my full feed or partial feed.

 

126 comments (Add Yours)

You can keep up to date with this discussion by subscribing to the RSS or Atom feed.
 
[#1] Mod_rewrite is soo much big and complicated... but the examples in the sheet are great! Thanks a lot (again? Yezz!!)
[#2] Thanks a lot again! Your cheat sheets are great - a lot of information. Until now there was Google, a "1000 pages PHP5"-book and so on...
Another thanks to you!!
[#3] Amazing. This is going to be really helpfull for a lot of people.
 Saudi Arabia May 11, 2005
[#4] I'll start a new website called iloveilovejackdaniels.com :D really I like all your Cheat Sheets.
Anonymous
United States May 12, 2005
[#5] once again, another great reference sheet. Any possibility of a PDF?
 United Kingdom May 12, 2005
[#6] Yup, every possibility. I'm working on it!

I've had a couple of problems getting a high quality PDF done (the cheat sheets were all created in XaraX). I should have something by the weekend though.
 United States May 12, 2005
[#7] QSA: "append", not "appeand". :-)
[#8] "I'll start a new website called iloveilovejackdaniels.com :D"

My same thoughts!

Thanks for providing a straight-to-the-point approach to all these stuff.

Thanks indeed, and keep them coming!
[#9] Funny this should pop up now. I was just looking around the other day to see if I could use Mod_rewrite to change the log files for each domain to be in the domain's folder. I don't see an answer right off, but I'm still working on it. Why you may ask? So I can run webalizer on each one for each customer to see what their site is doing.
 United States May 13, 2005
[#10] Yet another great cheat sheet! Can you do a MySQL one, too? That would ROCK!
 United Kingdom May 13, 2005
[#11] PDF version now online too :)

I've actually started a MySQL one already. Thing is that MySQL doesn't lend itself to a particularly user-friendly cheat sheet. I'm working on it though. It's no use if it can't be used as a quick reference - hopefully in the next couple of weeks I'll have that one done.
Anonymous
United States May 13, 2005
[#12] I like this (printing it now) and would love to see something like this for SQL or PostgreSQL.
 Netherlands May 13, 2005
[#13] Thanks! Finally a PDF version :) j00 r0x0rz! :p
Anonymous
United States May 14, 2005
[#14] this example is great:
# domain.com/articles/title/5/ to domain.com/article.php?name=title&page=5

could you do some more on similar "nice URL" topics :)
Johnny
Belgium May 14, 2005
[#15] Awesome man! It's all printed and put in plastic folders lying on my desk. Thanks
[#16] Thanks for the PDF! The PNG's gave me a bit of trouble when printing.
 United States May 15, 2005
[#17] Very nice. Thanks a lot!
[#18] Nice.

It's very helpful.
Great job !
 Russian Federation May 20, 2005
[#19] Thanks for taking the time to put this together Dave, very useful
 United Kingdom May 20, 2005
[#20] Great stuff, thanks for the recommendation, I've bookmarked that immediately.

Dan (via BNMlist)
 United Kingdom May 20, 2005
[#21] Thanks, everyone!
[#22] Thanks for this one. I've printed the previous double-sided and actually laminated them. That way I can keep them on my desk without them becoming filthy.

If you're done with the MySQL one I'll be doing the same with these two.

Much appreciated!
 United States May 23, 2005
[#23] I have been meaning to do this for sometime guess you just made it easier thanks!!
outstanding
United States May 23, 2005
[#24] fantastic, great job!
 United States May 24, 2005
[#25] This is by far, the best mod_rewrite resource that I've seen and I've been looking for almost a year. Thank you so much for making this sheet!
[#26] Hi,
great sheets, thank you. ;)
But I think there are small misstakes in the examples.
In example 1 and 2 the dot (.) in the regular expression is not escaped.

Example 1:
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
should be:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]

Example 2:
RewriteRule ^page.html$ new_page.htm [R,NC,L]
should be:
RewriteRule ^page\.html$ new_page.htm [R,NC,L]

The flag QSA would also be nice for this example. :)
 United States June 26, 2005
[#27] Hi, I tried to download this sheet in PNG and it said I couldn't due to errors.

"The image ‚€œhttp://www.ilovejackdaniels.com/mod_rewrite_cheat_sheet.png‚€Ě cannot be displayed, because it contains errors."

I'm hoping to get this, my wife would love it.

Thanks!
 Czech Republic July 15, 2005
[#28] Many thanks. I have been missing such a comprehensive yet very compact mod_rewrite companion.
Bob
Australia July 22, 2005
[#29] Great work!!!!!

Thank you so much for such an amazing summary sheet!
[#30] Thanks a lot for this cheat sheet it saved me a lot of time :-)
 United Kingdom September 11, 2005
[#31] While looking via Google for more examples on implementing mod_rewrite, I found your very helpful cheat sheet. Thanks very much for making it available.
[#32] This is a great reference guide. I was searching for some downloadable guide, and I found this cheat sheet. :) Have printed it out and its pretty useful! Thanks a bunch!
[#33] Thankyou, everyone. I appreciate the compliments!
[#34] Nifty!!!!!
 United States September 29, 2005
[#35] Thank You!!! I've been searching Google for hours to find this info.
[#36] I can't seem to rewrite my urls properly. If anyone is willing to help me get my urls rewritten properly, I will paypal you! Please contact me here: http://www.meldo.com/cms/contact.php

Great cheet sheet by the way.. I just can't get it right!
[#37] I am having the same problem and it doesn't help that we are using asp which, there is no rewrite for.
[#38] Hi "SimpleRent". There is a rewrite module for IIS (the server that usually runs ASP). It's called ISAPI_Rewrite.
[#39] Hmm.. I thought that was only for .net . I will look into that. Thanks!
Ethernia Inanov
Turkey October 29, 2005
[#40] Lovely, thanks a lot for this unique and detailed graphic work.
[#41] Thanks! I've already printed the sheet out, and I believe it'll help me greatly to have it handy, instead of googling for same things every time I want to create a new RewriteRule... :)

Thanks again!
Silvia
Italy November 15, 2005
[#42] I have found this tools:
<a href=http://www.cached.it/english/>http://www.cached.it/english/
</a>

for see how many page up with mod rewrite
 United Kingdom November 21, 2005
[#43] While the cheat sheet is great, for first time users, you could possibly do a "where to do teh rewrites". Took me a while to find out the first time I did it. Love your sheets.
[#44] The instructions are great. thx.
 United States December 12, 2005
[#45] Exactly what I needed!
[#46] Finally i have been able to something with mod_rewrit.

Thanx.
Mannan.
Muhammad Abdul Mannan
Pakistan December 30, 2005
[#47] mod_rewrite is so much big and complicated but some how i am able to understand bit of it.

thanx.
[#48] Great cheat sheet, but I can't do that it work for me, I have this rule:
RewriteRule ^tags/([A-Za-z0-9-]+)/?$ tags.php?name=$1 [L]
this file is on domain.com/phpapps/.htaccess I don't put htaccess file in domain.com/.htaccess beacuse I don't have access to this folder, but mod_rewrite on apache is on.
when I try to enter to this url: domain.com/phpapps/tags/name i get server error 500.
anyone help me?
Ryan G
Australia January 24, 2006
[#49] "Guanajuato City", have you enabled the rewrite engine? You won't get anything rewritten otherwise... That's the only part that was left out;

RewriteEngine On

Also, on my web server, entering the URL /test and /test.php return the same result. With a phpinfo(), it shows /test PHP_SELF as /test.php... Very odd. So I just changed the rewrite to a diff dir, /tests => /test.php.
 United Kingdom February 24, 2006
[#50] Thanks a lot for this have not got mod_rewrite running on my site.
 United States March 15, 2006
[#51] Thx for the cheat sheet! :) Maybe you have some insight on an issue I have... I have thousands of pages on my site... each in a different directory because they are image galleries...I am implementing a referral tracking system that passes information to the gallery page using a query string...

Example:
http://www.mydomain.com/gallery_tracker/galleries/group/user/01/index.php?ident=1&gal=1
http://www.mydomain.com/gallery_tracker/galleries/group/user/02/index.php?ident=2&gal=3

THE PROBLEM:
I have successfully used mod_rewrite to make this url SEF but it appears that I have to add a rule for every gallery due to the path difference. Is there a way around this? Can I match the path of any request in the RewriteRule so I don't have to add a RewriteRule for every path?
 United States March 15, 2006
[#52] I found an answer to my question... it involves using Regular Expressions to match the folders :)

http://wettone.com/code/clean-urls
[#53] I collect the information on htaccess and mod_rewrite, thanks a lot. Smile - you're being linked to :)
jalap
India April 18, 2006
[#54] your site is not working in IIS
 United Kingdom April 18, 2006
[#55] Hi jalap. Any chance you could provide some more information?
[#56] tip top
thanks
 United States May 12, 2006
[#57] An extremely useful cheat sheet. I would love to see this expanded.

Silk
 United States May 24, 2006
[#58] This is actually really useful, thanks for making it simple!
[#59] I am having the same problem and it doesn't help that we are using asp which, there is no rewrite for.
Mathieu
France June 5, 2006
[#60] Thanks... nice, simple and clear !
:)
Anonymous
United Kingdom June 21, 2006
[#61] Needs something about using !-f and !-d that sort of thing.
[#62] Has anyone successfully used mod_rewrite to remove jsessionid from URLs? The technique described here http://enarion.net/google/tricks/search-engine-cloaking/ removes all parameters fromt the URL, but I only want to remove the jsession parameter.
Richard Vaughan
United Kingdom July 21, 2006
[#63] A very good solution has been posted here for removing php sessions using mod_rewrite: http://www.oscommerce.com/community/contributions,2819

I have noticed a small performance hit but at least my SEO isn't crippled by session ids anymore!
Drakas
United Kingdom July 26, 2006
[#64] A good idea would be to create a regular expression cheat sheet :)
That would be very useful. Very.
 Czech Republic July 29, 2006
[#65] helpfull like hell; that's all I can say...
Berry
Romania August 1, 2006
[#66] Thanks man, makes my live easier!
Kevin
United States August 8, 2006
[#67] We need to make our URL short since we are running on Enginity intershop.

Will it make the urls short or does it only do re-directing.

Hope anyone can help me. Please feel free to call me at [removed]
 United Kingdom August 11, 2006
[#68] Thanks, this has been a huge help!
[#69] Excellent very handy. A good addition would be to explain what the symbols are eg, [] and () * etc.
 United States August 29, 2006
[#70] Very handy cheat sheet indeed. I may have to look into creating nice URLs for my site as well now... Always had some problems due to issues when trying to access a folder within the site... Maybe skipping x number of rules would be the answer.

Thank you.
[#71] thanks. very nice.
 Republic Of Moldova September 21, 2006
[#72] Just thanks,
now i have short and clean urls with no errors in apache errorlog!
Ayaz
France October 23, 2006
[#73] Thanks, I was trying for it from some time but was not able to use it.
[#74] Dave,

Many thanks for one of the more succinct yet comprehendible intros to using mod_rewrite. The above domain is my first venture into a PHP/MYSQL driven site and learning how to make SE friendly URLs has been, well, interesting! ;_)

Cheers!

Mike Shafer
[#75] http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/urlrewriting.asp

Great example, Perfect implementation of URL Rewriting. I did not write the article, but extended the code to query a database instead of the web.config file. Then, new pages can be tide to a content table in the database. Check out

http://www.collegecupcakes.com/Steph_253_Fun.aspx

to see it in action No querystrings are visible to the end user. Great for SEO and emailing the links to people. If you need the code to keep the URLs in the database intead of a config file, email me at sultan@collegecupcakes.com.

All the articles on the site are coded with URL rewriting, and dynamic metas. no more "index.aspx?ID=123". Plus, have you every tried to email a link in and then when the reciepient gets the link it is broken, because the mail program inserted line feeds? Having links like

http://www.collegecupcakes.com/Party_With_No_Hangover.aspx

Solves this problem.

The under scores prevent the email program from truncating lines and moving part of the querystring to he next line.

Email me if you have any questions about implementing the url rewriting. I can email you the class that queries the database so you can create urls on the fly.
[#76] Thnx for the MOD REWRITE rules. Really usefull !!!
André
Germany January 30, 2007
[#77] Great work... but, could it be that some pages are missing? there is only one page in the pdf while there is a lot more listed in the overview...

Kind regards!
[#78] Hi André,

No, it's a one-page thing. The thumbnails listed in the overview explain what is in each section of the cheat sheet.
[#79] Super handy, cheers :)
[#80] Very useful indeed. But do you know how to make my 'css' files located in some directory work in each direcory
seo
Belarus March 7, 2007
[#81] Cool post. I've digged billions of sites looking for that information.
This is very useful and may be used as a manual actually =)
 United States March 9, 2007
[#82] Very helpful post. I had been surfing all of the google search results and then I found this post. Great work and the PDF is now posted above my monitor. :~)
[#83] I got Regular Expression and mod_rewrite cheat sheet. Where i can get others ? Still under construction ? I can't find link in the post.
[#84] gjob, thank you.
 United Kingdom March 20, 2007
[#85] Great Work,

at last I have a better understading of mod re-write
keep up the good work..

Best regards from Alan
[#86] After 2 hours of search on the net to fix a nagging rewrite problem, i founnd this, and got the da..n thing working in a jiffy. Thanks!
vicious
Spain April 4, 2007
[#87] Simply awesome one :)
 United Kingdom April 6, 2007
[#88] I have one for your cheat sheet that is giving me a headache help would be great please..
RewriteRule ^Content/photo/([A-Za-z0-9-]+)/([0-9]+)/?$ Content/photo.php?prod=$1&cat=$2 [L]

Thank you...
[#89] RE: Alan Walker

If that rewrite rule is going in your httpd.conf (rather than an .htaccess file) you might try changing it like such:

RewriteRule ^/Content/photo/([A-Za-z0-9-]+)/([0-9]+)/?$ http://www.yourWebServerName.com/Content/photo.php?prod=$1&cat=$2 [L]

Give it a shot.
[#90] This has made a huge difference to my next online store. Not more ?category_id...........
Cheers
David
[#91] Hello everybody!
How can I rewrite my dynaic urls index.php?id=xx&page=xx for they to look as static ones: /id/page/.
Does anybody know? I did everything found on the net . But nothing works.

Thanks alot!
[#92] Great info, but a bit over my head.

Looking to redirect this...

http://www.agentswelcome.ca/component/option,com_ezrealty/Itemid,5/task,detail/id,7/

to this...

http://www.agentswelcome.ca/7

Can anyone help with this? Not adverse to paying for it.

THx!

tburchett<@>g.m.a.i.l.<com!>
[#93] thanks a lot for this
- useful
- handy
- compact
- convenient
cheat-sheet!
 United States May 3, 2007
[#94] Very helpful! Thank you! One stop quick reference to mod_rewrite.
 United States May 3, 2007
[#95] This is going to my tech bookmarks. Very handy. Thank you.
[#96] It is very helpful for beginners.
 United Kingdom May 11, 2007
[#97] Thanks - great help.
[#98] This site is interesting and very informative, nicely interface. Enjoyed browsing through the site
[#99] I think these blog is really useful for new comers! great article, thanks
 Netherlands May 12, 2007
[#100] Thx For this cheatsheet :P
SEO
Australia May 29, 2007
[#101] Excellent work, i redirected dynamic to static HTML and it does help alot.

On http://www.4rum.info
Rik
Belgium May 30, 2007
[#102] GREAT document!!!

I was reasonably experienced with rewriting before i found this, but i'm sure that even then it'll save me lots of time if i need to make sure of something ;)

nice work!
[#103] Wow! Fantastic work! What a great time saver!
SeoGem
Canada June 2, 2007
[#104] Rewriting urls is one of the most effective seo strategies. Thank you for your thoughtful work! I think I can say that for all webmasters!
[#105] Finally got it to work! Thanks for the useful info! Sorry, pardon my last post!
[#106] We use your cheat sheets as a quick reference all the time!
It makes our web development lives easier!
[#107] Thank you very much for this, it helped me a lot!
[#108] make a cheatsheet of deprecated code ;)
[#109] Thanks, your cheat sheets are very good and useful
 United States July 23, 2007
[#110] thanks your cheat sheet its very useful. and it neatly organized tk
 United States August 7, 2007
[#111] Indeed, thank you. Very handy and well organized cheat sheet.
Aussie job search
United Kingdom August 31, 2007
[#112] Thanks again man! Very useful information!
 United States September 10, 2007
[#113] Very useful, Thank you. Is the most completed info i've found. Thanks.
G0rt
United States September 12, 2007
[#114] Thanks for the help. Much appreciated..
Paul
Australia September 13, 2007
[#115] Just wanted to say thanks for all the awesome cheat sheets, they have helped me out many times.
[#116] I think these blog is really useful for new comers! great article, thanks
[#117] Extremely helpful, I thank you many times over. Even better than thanking you, I'm sending everybody I know who would benefit from this a link to your site.
[#118] I am on the way to learn it by myself. I found these cheat sheets are much useful to enhance my learning. Thanks a lot for sharing it with us.
[#119] Very nice cheatsheet, will come in handy often. Thanks for putting this up!
[#120] I'm having a problem with this:
the .htaccess (based from the cheat example with pagination):
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^blog/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ blog.php?section=$1&bpost=$2 [L]

when I try "mysite.com/blog" it works
but when I try "mysite.com/blog/" (with trailing slash) or "mysite.com/blog/blog-section/blog-post"

Does anyone know what's wrong?
Thanks in advance.
[#121] all your cheat sheets are very helpful.
thanks again for sharing it for free.
[#122] I just want you to know how much I appreciate your effort. This cheat sweet is a life saver. Thank you Jack Daniel's
[#123] Simply awesome one :)
[#124] Your cheat sheet helped me out today. I'm going to print it and post it in my office.
[#125] gabs, try this:
RewriteRule ^blog/([A-Za-z0-9]+)/([A-Za-z0-9]+)(\/)?$ blog.php?section=$1&bpost=$2 [L]
[#126] Thanks for the cheatsheet. Will surely save me lot of time. Much appreciated.

 

Post Your Comment

 
 

Live Comment Preview

 United States1 minute ago
[#127]

Charcoal-mellowed Development and Marketing