Mod_Rewrite Cheat Sheet, a quick reference guide for mod_rewrite, with rewrite flags, regular expression syntax and sample rules.
download mod_rewrite cheat sheet in pdf
download mod_rewrite cheat sheet in png
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.
