There is a newer version of this 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
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
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.
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.
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.
This section lists a few of the most situations mod_rewrite is used for, along with a rule to achieve the desired affect.
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.
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, please consider buying me something from my Amazon Wishlist. Thankyou very much to those who have already hunted it down and sent me something nice - I'm very grateful!