Overview
The mod_rewrite cheat sheet is a one-page reference sheet, listing flags for the RewriteRule and RewriteCond directives, list of server variables, a regular expression guide and several examples of common rules.
If you're new to URL rewriting with mod_rewrite, you might find my article on URL Rewriting for Beginners useful.
This is the second version of the mod_rewrite cheat sheet. The previous version can be found at http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet-version-1/.
If you like the cheat sheets, and want to say thanks, please consider buying me something from my Amazon Wishlist. Thankyou very much to those who have already hunted it down and sent me something - I'm very grateful!
Downloads
The mod_rewrite Cheat Sheet is released under a Creative Commons License (Attribution, Non-Commercial, Share Alike).
Please note: If you wish to link to a cheat sheet from elsewhere, please link to this page so others find all available versions, the license and the description.
What's New?
There are a few small changes from the first version of the mod_rewrite Cheat Sheet (which you can still download if you prefer). The most obvious change may be that it now looks different. Hopefully it's now clearer and a little easier to find the information you're looking for.
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.
RewriteRule and RewriteCond 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.
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.
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.
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
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
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.

84 Comments
Cool. This looks good. We use this as part of our web delivery framework. Will make a copy and pin it up for future reference.
#1, Olmec Sinclair, New Zealand, 2 July 2008. Reply to this.
I do tech support for a webhosting company. I use your mod_rewrite cheat sheets all the time.
#2, David Maas, United States, 9 July 2008. Reply to this.
Amazing! thanks a lot for this guide.
#3, Hari, Unknown, 14 July 2008. Reply to this.
This is a great reference guide. These cheat sheets do save our time and brain.. Have printed it out and its pretty useful! Thx very much!
#4, Bob, Unknown, 21 July 2008. Reply to this.
Thanks for this! Anything to make it easier to traverse mod_rewrite voodoo.
#5, James, United States, 22 July 2008. Reply to this.
Thanks everyone - you're all welcome. :)
#6, Dave Child, United Kingdom, 23 July 2008. Reply to this.
Hey,
Thanks... I too work for a hosting company and refer people to your site all the time. Thanks!
#7, Mike Piekarski, United States, 30 July 2008. Reply to this.
Thanks for the cheat sheet Dave. The flag list is particularly helpful. I started off with an example like your "Nice Looking URLs" in working with a recent site. I think many real world situations are going to need something more complex, or take the seo hit to put "category" and "page" in the url to key off of in a one line rewrite.
This example rewrites the urls for http://www.aslaninteractive.com
RewriteCond %{REQUEST_FILENAME}.cfm -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^portfolio/([a-zA-Z0-9-_]+)$ /portfolio/$1.cfm [PT,L]
RewriteRule ^services/([a-zA-Z0-9-_]+)$ /services/$1.cfm [PT,L]
RewriteRule ^success_stories/([a-zA-Z0-9-_]+)$ /success_stories/$1.cfm [PT,L]
RewriteRule ^/?([a-zA-Z0-9-_]+)$ $1.cfm [PT,L]
#8, Matt MacDougall, Unknown, 10 August 2008. Reply to this.
I'm working on Wordpress and the client wants a spash screen (ugh) and I need to go to index.html if they go to the root of the website (url only, and no filename afterward) but for all else (any file, or directory) goes to index.php. I can't seem to figure out how to do it. I figured out how to map index.html to index.html (overcoming all going to index.php) but can't get the root to redirect. Any assistance would be greatly appreciated. The default WP .htaccess file with my mod:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/index.html$ index.html [C]
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>
#9, Samantha, Unknown, 14 August 2008. Reply to this.
Hi,
Excellent tutorial. I will refer people to your site.
#10, Mezbaur Rahman, United Kingdom, 14 August 2008. Reply to this.
this sheet is awesome, help me so much
#11, Bali Web Developer, Indonesia, 17 August 2008. Reply to this.
Damm Good. It gives quick insight . Very Very Helpfull .
#12, Dilip Kumar, United States, 5 September 2008. Reply to this.
Sorry for my English, that it is not good, wanted to know if by means of rewrite it is possible to remove links broken from a Web, in fact is a vestibule of sales that takes links from another Web, therefore is impossible to annul them manually, places the Web to you in question, has 4 there but I do not put them not to make Spam, thanks
#13, Juan Francisco Segui, Unknown, 10 October 2008. Reply to this.
Hey I like the new additions to the cheat-sheet... It somewhat inspired me to document the various mod_rewrite variables at http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html
I've found it really helpful while I'm messing with .htaccess stuff- I keep a printed copy on my desk!
#14, AskApache, United States, 11 October 2008. Reply to this.
Woahh good resource! Thx you very much...
#15, leoslab, Indonesia, 17 October 2008. Reply to this.
Very educative, indeed! I'm currently working on a script and I found this tutorial/guide very useful.
#16, Mexabet, Australia, 21 October 2008. Reply to this.
I need an cheat
#17, Victorboy, United States, 31 October 2008. Reply to this.
Useful. Thanks
#18, guatejon, Honduras, 6 November 2008. Reply to this.
Really great resource. I posted a link here to share your doc with other people: http://phireitup.blogspot.com/2008/11/modrewrite-cheat-sheet.html
#19, SaidK, Unknown, 6 November 2008. Reply to this.
This helped me a lot to crack the nut :)
thanks a lot my dear friend
keep the good work moving...
thnx again
#20, samil, Unknown, 14 November 2008. Reply to this.
Amazing! thanks a lot for this guide.
#21, Automation, Unknown, 15 November 2008. Reply to this.
Thank You very much. It's not easy for me, to understand the rewrite rules. But they are necessary for a webworker.
#22, Gerd-E., Germany, 27 November 2008. Reply to this.
Good mod_rewrite sheet, related to url-rewriting-for-beginners in your article.
Very useful ...
#23, Myea Amelia, Indonesia, 28 November 2008. Reply to this.
Amazing effort! I am wondering how to use mod_rewrite with Rails mongrel?
Thanks!
#24, Alan Murary, Unknown, 11 December 2008. Reply to this.
This is an awesome site.
#25, J.T., Jordan, 11 December 2008. Reply to this.
Thank you very much for this valuable sheet.
Regards.
#26, Maksin, Unknown, 24 December 2008. Reply to this.
Nothing like cheatin' to get something done faster!
#27, Rimian, Australia, 8 January 2009. Reply to this.
Thank you very much for this very helpful cheat sheets!
#28, Saeed, Unknown, 12 January 2009. Reply to this.
Excellent! mod_rewrite rules
#29, ed neville, United Kingdom, 12 January 2009. Reply to this.
this example not work <IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/index.html$ index.html [C]
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>
#30, Luiss, colombia, 14 January 2009. Reply to this.
I was looking for a summary like that, many thanks!
#31, Alex from Nürnberg, Unknown, 29 January 2009. Reply to this.
Hi,
I am using mod_rewrite in my web site and getting following issue.
My root directory path
/xxx/yyy/public_html/
All my files website present in ?official? folder (i-e) /xxx/yyy/public_html/official/
.htaccess file present in root directory (i-e) /xxx/yyy/public_html/
Script present in .htaccess file.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^official/browse/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ official/index.php?do=browse&s=$1&ss=$2 [NC,L] # Process browse requests
RewriteRule ^official/browse/([A-Za-z0-9-]+)/?$ official/index.php?do=browse&s=$1 [NC,L] # Process browse requests
RewriteRule ^official/([A-Za-z-]+)/?$ official/index.php?do=$1 [NC,L] # Process product requests
Script present in browse.php
<a href="index.php?do=browse&s=<?php echo $sect['id']; ?>&ss=<?php echo $ssect['id']; ?>"><strong><?php echo $ssect['subname']; ?></strong></a>
My issue
At first visit of browse page it works fine
(i-e) http://www.mydomain.net/official/browse/1/1/
If I click another section in browse page, it shows ?The page cannot be found? because of /browse/1/2/ added after /browse/1/1/
(i-e) http://www.mydomain.net/official/browse/1/1/browse/1/2/
Please give your suggestion to over come this problem and waiting for your valuable reply.
Thanks in advance.
Manikandan.S
#32, Manikandan, Unknown, 29 January 2009. Reply to this.
Some great information, another good site that I have found that may be of interest to your users is this site that has a mod rewrite generator: http://www.generateit.net/mod-rewrite/
#33, Dave Simpson, Unknown, 8 February 2009. Reply to this.
hey, this cheat is so awesome. many thanks!
#34, hal?, turkey, 14 February 2009. Reply to this.
That's what I really wanted, all info in one place! This is really useful.
#35, Abhijeet Pathak, India, 21 February 2009. Reply to this.
very good guia
#36, Bilaz, Brazil, 1 March 2009. Reply to this.
slightly confussed on a few things! and with my eye sight chances are I've miss read something somewhere.
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?pagename=$1
gives me the nice urls.
but I have a form which resides in /contact/ and all links are setup as mysite
mysite/about/
and so on and when it gets to mysite/contact/
it tries to find the pagename from the database. how do I get to to redirect to that folder so it will pick the form up?
As I sida I think i've missed something somewhere. hope someone is kind enough to HELP.
Regards
Mally
#37, mally, United Kingdom, 2 March 2009. Reply to this.
Hey! this help me so much ! tanks!
#38, Vini, Brazil, 4 March 2009. Reply to this.
Thanks for the article.
I used to mod_rewrite in .htaccess files im my site http://www.otimizacao-de-websites.com , but using cheat sheet to rewrite is great new to me!
#39, Marcos, Brazil, 12 March 2009. Reply to this.
this helped me a lot. thanks.
#40, web dizayn, Unknown, 13 March 2009. Reply to this.
phew! just what i was looking for... bookmarked :)
#41, Mr. G, Unknown, 24 March 2009. Reply to this.
Hej Thank you for your great work. This will help me alot.. Printed on A3+ :)
#42, Michael, Kosovo, 2 April 2009. Reply to this.
Thanks for this! I got what I was looking for in 1 minute!
#43, Emprestimo Pessoal, Germany, 9 April 2009. Reply to this.
Nice! Glad you put it into a nice printable form. Will have to get this laminated.
#44, Tim Sabulis, Unknown, 14 April 2009. Reply to this.
Yeah, that was what i searching for. Now it works fine on my site, Thank you.
#45, Barney, Unknown, 14 May 2009. Reply to this.
Good resource!
#46, Martin, Uruguay, 18 May 2009. Reply to this.
Good post!
Liked and back other time
indicate to my friends
cya!
#47, Moda, Unknown, 26 May 2009. Reply to this.
Great, i`m still working on a htaccess file. Now i can work faster. I`m a beginner with mod rewrite but i`m geting better.
#48, Auspuff, Germany, 8 June 2009. Reply to this.
Wow. What a great post!!! I'm downloading your cheatsheat now. Thanks for share it with us.
#49, Ivan Kristianto, Australia, 28 June 2009. Reply to this.
Hey, Thank you very much for this very helpful info!
#50, Kubotan, Unknown, 29 June 2009. Reply to this.
thanks for sharing this, I was looking for some cheat sheet like this.
keep on the good work. thank you.
#51, Veyton, Unknown, 7 July 2009. Reply to this.
This is awesome, I just downloaded you regular Expression sheet. These are very helpful!
#52, Dwayne, Shasta Lake CA, 21 July 2009. Reply to this.
Super guide!
#53, Trockenfutter, Germany, 22 July 2009. Reply to this.
Amazing! thanks a lot for this guide. Very helpfull, good work.
#54, Haarentfernung, Germany, 25 July 2009. Reply to this.
Yes a good guide
#55, Geldanlage-Vergleich, Germany, 26 July 2009. Reply to this.
This is an excellent overview but I'd like to make just a couple of recommendations:
1. The No Case flag should RARELY be used in a RewriteRule - it's purpose is to match case INsensitive characters such as those in the {HTTP_HOST}. If they're used in a RewriteRule to capture in an Apache $n variable, the receiving script must then convert the case to be usable. Think about WHO is creating the URI for the mod_rewrite to convert and ask WHY create a LOT more work for yourself.
2. The Last flag ... and the start anchor difference in the two versions of Apache (^/ for Apache 1.x, ^ for Apache 2.x, ^/? for both). You've shown only Apache 2's version with no directions for change for Apache 1.x.
If you ever feel the need to go to a second page, examples would help people learn a bit more about how these code snippets are used. I have a few at the bottom of the horribly long tutorial at http://datakoncepts.com/seo.
#56, DK, New Zealand, 4 August 2009. Reply to this.
A very useful cheat sheet.
#57, rinoplastia, Spain, 13 August 2009. Reply to this.
I think mod rewrite is really complecated. Thanks for this article.
#58, haister, Unknown, 13 August 2009. Reply to this.
this seems to be very usefull. Thanks for sharing this.
#59, Miyuki, Germany, 20 August 2009. Reply to this.
Great work! Very nice site.
#60, Geberstange, Germany, 26 August 2009. Reply to this.
Thank for sharing this excellent mod_rewrite guide!
#61, Spachtel, Germany, 1 September 2009. Reply to this.
Thank you. It's not easy for me, to understand the rewrite rules. But they are necessary for a webworker.
cu
#62, Chris Trick, Germany, 4 September 2009. Reply to this.
A nice work, like your other cheat sheets. A big thanks for sharing thar guide from Kinderbuch.
#63, Kinderbuch, Unknown, 7 September 2009. Reply to this.
Hi,
The mod rewrite rule works perfectly. The page redirect by getting the variable and uses it right. but it does not pint the actual url in address bar
http://www.divinequran.com/koran/1-Al-Fatiha.html
It has to display the above url in the address bar, Instead of it displays the rewrite code as it is. I have used option L but it doesnot works.
#64, mathguy, India, 28 September 2009. Reply to this.
Ooough.. little bit confuse...
#65, Fisika SMA, Indonesia, 5 October 2009. Reply to this.
Super brilliant reference book! With the help of the "cheat sheet" can I save my various data faster. The best example is the "flag list"
Thanks for the great cheat sheet, Dave!
cu
#66, Andreas, Unknown, 7 October 2009. Reply to this.
I'm glad I found this cheat sheet. It has been helpful to me while rewriting the dynamically-generated URLs of my photo gallery. Thanks, Dave.
#67, PHP Gallery, Australia, 14 October 2009. Reply to this.
This is one of the best mod rewrite cheat sheets I've found.
#68, Cesc Fabregas, Australia, 25 October 2009. Reply to this.
Even with all the articles and tutorials I've seen on the net, Mod_Rewrite is still a voodoo. I hope this cheat sheet will help to rewrite my URLs.
#69, Kyle, Ireland, 25 October 2009. Reply to this.
This is a great reference guide. These cheat sheets do save our time and brain.. Have printed it out and its pretty useful! Thx very much!
#70, Fussmatten, Germany, 27 October 2009. Reply to this.
Great reference-sheet, which saves a lot of time. Thanks for sharing it, it's one of the best cheat-sheets that are in the net.
#71, Weihnachtsgeschenke, Germany, 9 November 2009. Reply to this.
Hi guys,
I'm a bit stuck... I'm moving articles that were published using a custom CMS and contained capitalized links to running wordpress as CMS.
Now WP doesn't allow capitalization in links, but there are links all over the net pointing to my articles.
Is there a rewrite rule that enable me to say ignore the case of any link coming in to the directory /articles and make it all lower case?
Can I also include to ignore the ending, ie .html, .php, ect...
Thanks for your help!
Veit
#72, Veit, Australia, 20 November 2009. Reply to this.
Thank You very much for that great sheet. It's really hard for me, to understand that rewrite rules. But from time to time I really need them.
#73, Birger, Unknown, 20 November 2009. Reply to this.
Thanks a lot for this guide.
#74, Roman, Germany, 8 December 2009. Reply to this.
Thanks for the effort and time you put in to make these sheets available. They make things a lot easier.
Andy :-)
#75, Andy Kuiper, Canada, 17 December 2009. Reply to this.
Thank for sharing this excellent mod_rewrite guide
#76, Angelreisen, Germany, 23 December 2009. Reply to this.
Nice guide, I also used this instructions on my website: <a href="www.nanunikokoritu.org">www.nanunikokoritu.org</a>
#77, Erik C., Brazil, 23 December 2009. Reply to this.
Really useful stuff.
Cool and handy rewrite cheat sheet.
Thank you.
#78, wiserx, 24 December 2009. Reply to this.
hi Dave.
can see you've heard it all before here, but we have been periodically struggling with various htaccess needs on and off for years now, wish i'd seen these before..
thanks for posting and regards from Ibiza
kev grant
#79, kev grant, ibiza, 17 January 2010. Reply to this.
Mod rewrite is a very difticult task. These sheets helps much.
#80, Kreditvergleich Wissen, Germany, 19 January 2010. Reply to this.
Please give your suggestion to over come this problem and waiting for your valuable reply.
Thank you
#81, Wärmepumpe, Germany, 7 February 2010. Reply to this.
Please give your suggestion to over come this problem and waiting for your valuable reply.
Thank you
#82, Wärmepumpe, Germany, 7 February 2010. Reply to this.
Please give your suggestion to over come this problem and waiting for your valuable reply.
Thank you
#83, Wärmepumpe, Germany, 7 February 2010. Reply to this.
I am running a blog site that was converted over from a static html site. I have an htaccess file in place to 1) rewrite posts, 2) redirect old pages to new pages.
My problem is that everything works fine except for simply redirecting www.mydomain.com/index.html to www.mydomain.com/
Here is what my htaccess file looks like, can you help me fix it? I wonder if it has something to do with the first rewrite rule for index.php?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteRule ^index\.html$ http://www.mydomain.com/ [R,L] - This is the one that does not work
RewriteRule ^page1\.html$ http://www.mydomain.com/page1/ [R,L]
RewriteRule ^page2\.html$ http://www.mydomain.com/ [R,L]
RewriteRule ^page3\.html$ http://www.mydomain.com/page3/ [R,L]
RewriteRule ^page4\.html$ http://www.mydomain.com/page4/ [R,L]
RewriteRule ^page5\.html$ http://www.mydomain.com/page5/ [R,L]
#84, Atlanta Web Design, 8 February 2010. Reply to this.