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, 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.