Skip Navigation

mod_rewrite Cheat Sheet (V2)

Overview

mod_rewrite Cheat Sheet Version 2 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, 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

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.

RewriteRule and RewriteCond 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.

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

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.

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.

Other Formats

50 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.
David Maas
United States #2: July 9, 2008
I do tech support for a webhosting company. I use your mod_rewrite cheat sheets all the time.
Amazing! thanks a lot for this guide.
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!
 United States #5: July 22, 2008
Thanks for this! Anything to make it easier to traverse mod_rewrite voodoo.
Thanks everyone - you're all welcome. :)
Hey,

Thanks... I too work for a hosting company and refer people to your site all the time. Thanks!
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]
Samantha
USA #9: August 14, 2008
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>
Hi,

Excellent tutorial. I will refer people to your site.
this sheet is awesome, help me so much
Dilip Kumar
United States #12: September 5, 2008
Damm Good. It gives quick insight . Very Very Helpfull .
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
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!
Woahh good resource! Thx you very much...
Very educative, indeed! I'm currently working on a script and I found this tutorial/guide very useful.
I need an cheat
Useful. Thanks
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
samil
Unknown #20: November 14, 2008
This helped me a lot to crack the nut :)
thanks a lot my dear friend

keep the good work moving...

thnx again
Amazing! thanks a lot for this guide.
Thank You very much. It's not easy for me, to understand the rewrite rules. But they are necessary for a webworker.
Good mod_rewrite sheet, related to url-rewriting-for-beginners in your article.
Very useful ...
Amazing effort! I am wondering how to use mod_rewrite with Rails mongrel?
Thanks!
This is an awesome site.
Thank you very much for this valuable sheet.

Regards.
Nothing like cheatin' to get something done faster!
Saeed
Unknown #28: January 12, 2009
Thank you very much for this very helpful cheat sheets!
Excellent! mod_rewrite rules
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>
I was looking for a summary like that, many thanks!
Manikandan
Unknown #32: January 29, 2009
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

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/
hey, this cheat is so awesome. many thanks!
Abhijeet Pathak
India #35: February 21, 2009
That's what I really wanted, all info in one place! This is really useful.
Bilaz
Brazil #36: March 1, 2009
very good guia
mally
United Kingdom #37: March 2, 2009
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


Hey! this help me so much ! tanks!
Marcos
Brazil #39: March 12, 2009
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!
this helped me a lot. thanks.
phew! just what i was looking for... bookmarked :)
Hej Thank you for your great work. This will help me alot.. Printed on A3+ :)
Thanks for this! I got what I was looking for in 1 minute!
Nice! Glad you put it into a nice printable form. Will have to get this laminated.
Yeah, that was what i searching for. Now it works fine on my site, Thank you.
Good resource!
Good post!
Liked and back other time
indicate to my friends
cya!
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.
Wow. What a great post!!! I'm downloading your cheatsheat now. Thanks for share it with us.
Hey, Thank you very much for this very helpful info!

Post Your Comment

· Comments with keywords instead of a name have their URLs removed.
· Your email address will not be displayed or shared.

Live Comment Preview

 United States #51: 1 minute ago