mod_rewrite Cheat Sheet
by Dave Child (DaveChild) via cheatography.com/1/cs/4/
mod_rewrite Tutorials Redirection Header Codes mod_rewrite Server Variables: HTTP
Headers
http://httpd.apache.org/docs/current/rewrite/ 301 Moved permanently
http://www.addedbytes.com/for-beginn‐ 302 Moved temporarily (default) %{HTTP_USER_AGENT}
ers/url-rewriting-for-beginners/ %{HTTP_REFERER}
http://net.tutsplus.com/tutorials/other/a-dee‐ mod_rewrite Directives %{HTTP_COOKIE}
per-look-at-mod_rewrite-for-apache/ RewriteEngine RewriteMap %{HTTP_FORWARDED}
RewriteOptions RewriteBase %{HTTP_HOST}
mod_rewrite RewriteRule Flags
RewriteLog RewriteCond %{HTTP_PROXY_CONNECTION}
C Chained with next rule
RewriteLogLevel RewriteRule %{HTTP_ACCEPT}
CO=cookie Set specified cookie
RewriteLock
E=var:‐ Set environmental variable mod_rewrite Server Variables: Server
value “var” to “value” Regular Expressions Syntax Internals
F Forbidden (403 header) %{DOCUMENT_ROOT}
^ Start of string
G Gone - no longer exists %{SERVER_ADMIN}
$ End of string
H=handler Set handler %{SERVER_NAME}
. Any single character
L Last - stop processing rules %{SERVER_ADDR}
(a|b) a or b
N Next - continue processing %{SERVER_PORT}
(...) Group section
NC Case insensitive %{SERVER_PROTOCOL}
[abc] In range (a, b or c)
NE Do not escape output %{SERVER_SOFTWARE}
[^abc] Not in range
NS Ignore if subrequest
\s White space
P Proxy mod_rewrite Sample Rule: Site Moved
a? Zero or one of a
PT Pass through # Site moved permanently
a* Zero or more of a
R[=code] Redirect to new URL, with RewriteCond %{HTTP_HOST} ^www.doma‐
a*? Zero or more, ungreedy
optional code (see below) in.com$ [NC]
a+ One or more of a
QSA Append query string RewriteRule ^(.*)$ http://www.doma‐
a+? One or more, ungreedy
in2.com/$1 [R=301,L]
S=x Skip next x rules
a{3} Exactly 3 of a
T=mime- Set mime type Rewrites domain.com to domain2.com
a{3,} 3 or more of a
type
a{,6} Up to 6 of a mod_rewrite Sample Rule: Temporary Page
mod_rewrite RewriteCond Flags a{3,6} 3 to 6 of a Move
NC Case insensitive a{3,6}? 3 to 6 of a, ungreedy # Page has moved temporarily
OR Combine with next rule using 'OR' \ Escape character RewriteRule ^page.html$ new_page.html
instead of the default of 'AND' [R,NC,L]
[:punct:] Any punctuation symbol
[:space:] Any space character Rewrites domain.com/page.html to
domain.com/new_page.html
[:blank:] Space or tab
There's an excellent regular expression
tester at: http://regexpal.com/
By Dave Child (DaveChild) Published 19th October, 2011. Sponsored by Readable.com
cheatography.com/davechild/ Last updated 25th February, 2020. Measure your website readability!
aloneonahill.com Page 1 of 2. https://readable.com
by Dave Child (DaveChild) via cheatography.com/1/cs/4/
mod_rewrite Tutorials Redirection Header Codes mod_rewrite Server Variables: HTTP
Headers
http://httpd.apache.org/docs/current/rewrite/ 301 Moved permanently
http://www.addedbytes.com/for-beginn‐ 302 Moved temporarily (default) %{HTTP_USER_AGENT}
ers/url-rewriting-for-beginners/ %{HTTP_REFERER}
http://net.tutsplus.com/tutorials/other/a-dee‐ mod_rewrite Directives %{HTTP_COOKIE}
per-look-at-mod_rewrite-for-apache/ RewriteEngine RewriteMap %{HTTP_FORWARDED}
RewriteOptions RewriteBase %{HTTP_HOST}
mod_rewrite RewriteRule Flags
RewriteLog RewriteCond %{HTTP_PROXY_CONNECTION}
C Chained with next rule
RewriteLogLevel RewriteRule %{HTTP_ACCEPT}
CO=cookie Set specified cookie
RewriteLock
E=var:‐ Set environmental variable mod_rewrite Server Variables: Server
value “var” to “value” Regular Expressions Syntax Internals
F Forbidden (403 header) %{DOCUMENT_ROOT}
^ Start of string
G Gone - no longer exists %{SERVER_ADMIN}
$ End of string
H=handler Set handler %{SERVER_NAME}
. Any single character
L Last - stop processing rules %{SERVER_ADDR}
(a|b) a or b
N Next - continue processing %{SERVER_PORT}
(...) Group section
NC Case insensitive %{SERVER_PROTOCOL}
[abc] In range (a, b or c)
NE Do not escape output %{SERVER_SOFTWARE}
[^abc] Not in range
NS Ignore if subrequest
\s White space
P Proxy mod_rewrite Sample Rule: Site Moved
a? Zero or one of a
PT Pass through # Site moved permanently
a* Zero or more of a
R[=code] Redirect to new URL, with RewriteCond %{HTTP_HOST} ^www.doma‐
a*? Zero or more, ungreedy
optional code (see below) in.com$ [NC]
a+ One or more of a
QSA Append query string RewriteRule ^(.*)$ http://www.doma‐
a+? One or more, ungreedy
in2.com/$1 [R=301,L]
S=x Skip next x rules
a{3} Exactly 3 of a
T=mime- Set mime type Rewrites domain.com to domain2.com
a{3,} 3 or more of a
type
a{,6} Up to 6 of a mod_rewrite Sample Rule: Temporary Page
mod_rewrite RewriteCond Flags a{3,6} 3 to 6 of a Move
NC Case insensitive a{3,6}? 3 to 6 of a, ungreedy # Page has moved temporarily
OR Combine with next rule using 'OR' \ Escape character RewriteRule ^page.html$ new_page.html
instead of the default of 'AND' [R,NC,L]
[:punct:] Any punctuation symbol
[:space:] Any space character Rewrites domain.com/page.html to
domain.com/new_page.html
[:blank:] Space or tab
There's an excellent regular expression
tester at: http://regexpal.com/
By Dave Child (DaveChild) Published 19th October, 2011. Sponsored by Readable.com
cheatography.com/davechild/ Last updated 25th February, 2020. Measure your website readability!
aloneonahill.com Page 1 of 2. https://readable.com