Module mod_rewrite – URL Rewriting Engine – Cheat Sheet

This module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly. It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule to provide a really flexible and powerful URL manipulation mechanism. The URL manipulations can depend on various tests, for instance server variables, environment variables, HTTP headers, time stamps and even external database lookups in various formats can be used to achieve a really granular URL matching.

Regular Expressions Syntax:

Regular Expression Syntax

RewriteRule Flags
^
$
.
(a|b)
(…)
[abc]
[^abc]
\s
a?
a*
a*?
a+
a+?
a{3}
a{3,}
a{3,6}
a{3,6}?
!(pattern)
Start of string
End of string
Any single character
a or b
Group section
Item in range (a, b or c)
Not in range (not a, b or c)
White space
Zero or one of a
Zero or more of a
Zero or more of a, ungreedy
One or more of a
One or more of a, ungreedy
Exactly 3 of a
3 or more of a
3 to 6 of a
3 to 6 of a, ungreedy
“Not” prefix. Apply rule if
pattern does not match.
R[=code]  :  Redirect to new URL, with
optional code (see below).
F : Forbidden (sends 403 header)
G : Gone (no longer exists)
P : Proxy
L:  Last Rule
N :  Next (i.e. restart rules)
C : Chain
T=mime-type : Set Mime Type
NS : Skip if internal sub-request
NC : Case insensitive
QSA : Append query string
NE : Do not escape output
PT : Pass through
S=x : Skip next x rules
E=var:value : Set environmental variable
“var” to “value”.
Redirection Header Codes Server Variables: HTTP Headers
301
302
403
404
410
Moved permanently
Moved temporarily
Forbidden
Not Found
Gone
%{DOCUMENT_ROOT}
%{SERVER_ADMIN}
%{SERVER_NAME}
%{SERVER_ADDR}
%{SERVER_PORT}
%{SERVER_PROTOCOL}
%{SERVER_SOFTWARE}
RewriteCond Flags Server Variables: Special
NC
OR
Case insensitive
Combines rules with logical
“or” instead of “and”.
%{API_VERSION}
%{THE_REQUEST}
%{REQUEST_URI}
%{REQUEST_FILENAME}
%{IS_SUBREQ}
Directives Server Variables: Request
RewriteEngine
RewriteOptions
RewriteLog
RewriteLogLevel
RewriteLock
RewriteMap
RewriteBase
RewriteCond
RewriteRule
%{REMOTE_ADDR}
%{REMOTE_HOST}
%{REMOTE_USER}
%{REMOTE_IDENT}
%{REQUEST_METHOD}
%{SCRIPT_FILENAME}
%{PATH_INFO}
%{QUERY_STRING}
%{AUTH_TYPE}
Server Variables: HTTP Headers Server Variables: Time
%{HTTP_USER_AGENT}
%{HTTP_REFERER}
%{HTTP_COOKIE}
%{HTTP_FORWARDED}
%{HTTP_HOST}
%{HTTP_PROXY_CONNECTION}
%{HTTP_ACCEPT}
%{TIME_YEAR}
%{TIME_MON}
%{TIME_DAY}
%{TIME_HOUR}
%{TIME_MIN}
%{TIME_SEC}
%{TIME_WDAY}
%{TIME}

Example: New domain
#domain.com to domain2.com
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]
Example: Page moved temporarily
# domain.com/page.htm to domain.com/new_page.htm
RewriteRule ^page.htm$ new_page.htm [R,NC,L]
Example: Nice looking URLs (no querystring)
# domain.com/category-name/ to domain.com/categories.php?name=category-name
RewriteRule ^([A-Za-z0-9-]+)/?$ categories.php?name=$1 [L]
Example: Block Referrer Spam (where referring URL contains “viagra” or “xxx” )
RewriteCond %{HTTP_REFERER} (viagra) [NC,OR]
RewriteCond %{HTTP_REFERER} (xxx) [NC]
RewriteRule .* – [F]