Show Menu
Cheatography

Apache mod_rewrite Cheat Sheet (DRAFT) by [deleted]

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Links

Rewrit­eRule syntax

Rewrit­eRule Pattern Substi­tution [flags]

Rewrit­eRule substi­tutions

path
When rule is configured in server context and file system path does exist
URL
A relative path to the resource to be served
-
Do not substitute anything
$N
Reference to capturing group in
Rewrit­eRule
pattern
%N
Reference to capturing group in
Rewrit­eCond
%{VARNAME}
Server variable
${map:key|default}
Mapping function

Rewrit­eRule flags

B
Escape non-al­pha­num­erical characters before substi­tution
BNP
Escape space as %20 and not +
C
Chain the rules. It this rule fails, the chained one will be skipped
CO
Set a cookie
DPI
Discard path info of the URL
END
Stop all rewriting imeedi­ately
F
Return 403 Forbidden
G
Return 410 Gone
H
Send to specific content handler
L
Stop rewriting on this rule
N
Re-run rewriting from the first rule
NC
Case-i­nse­nsitive
NE
Do not hex-escape special characters
NS
Skip rule if it's a subrequest
P
PT
QSA
Add query string
QSD
Discard query string
QSL
Treat the last ? as QS delimiter
R
Redirect with code
S
Skip next N rules
T
Set the MIME-type
Multiple flags should be comma-­sep­arated
 

Logging

mod_re­write
logging can be enabled in apache config­uration file with
LogLevel
directive. Possible levels are from
trace1
to
trace8
. Log levels higher than 2 should be used only for debugging.
Config example:
LogLevel alert rewrit­e:t­race3

Directives

RewriteBase
specifies the URL prefix to be used for per-di­rectory (htaccess)
Rewrit­eRule
directives that substitute a relative path
RewriteCond
These directives may precede
Rewrit­eRule
. For rule that matches URL it will trigger rewriting only if all it's conditions evaluate to true
RewriteEngine
Enable or disable rewriting
RewriteMap
Used for values mapping in substi­tutions of
Rewrit­eRule
. Map values from files, databases, external functions etc.
RewriteOptions
Specifies rewriting options for server of directory
RewriteRule
Manch the URL, make substi­tut­ion(s). This directive only used for matching against URL path. For matching port, scheme, query string use
Rewrit­eCond