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

23 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
bangalore #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 ...

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 #24: 1 minute ago