Mod_rewrite Cheat Sheet

ADVERTISEMENT

mod_rewrite Cheat Sheet
by
Dave Child (DaveChild)
via
mod_rewrite Tutorials
Regular Expressions Syntax
mod_rewrite Sample Rule: Site Moved
^
Start of string
# Site moved permanently
$
End of string
RewriteCond %{HTTP_HOST} ^
rewriting-for-beginners/
[NC]
.
Any single character
RewriteRule ^(.*)$
(a|b)
a or b
at-mod_rewrite-for-apache/
[R=301,L]
(...)
Group section
Rewrites to
[abc]
In range (a, b or c)
mod_rewrite RewriteRule Flags
[^abc]
Not in range
C
Chained with next rule
mod_rewrite Sample Rule: Temporary Page Move
\s
White space
CO=cookie
Set specified cookie
# Page has moved temporarily
a?
Zero or one of a
E=var:value
Set environmental variable “var” to
RewriteRule ^page.html$ new_page.html [R,NC,L]
a*
Zero or more of a
“value”
Rewrites to
a*?
Zero or more, ungreedy
F
Forbidden (403 header)
a+
One or more of a
G
Gone - no longer exists
a+?
One or more, ungreedy
mod_rewrite Sample Rule: Nice URLs
H=handler
Set handler
a{3}
Exactly 3 of a
L
Last - stop processing rules
# Nice URLs (no query string)
a{3,}
3 or more of a
N
Next - continue processing
RewriteRule ^([A-Za-z0-9-]+)/?$ categories.php?
name=$1 [L]
a{,6}
Up to 6 of a
NC
Case insensitive
a{3,6}
3 to 6 of a
Rewrites to
NE
Do not escape output
a{3,6}?
3 to 6 of a, ungreedy
NS
Ignore if subrequest
1
\
Escape character
P
Proxy
[:punct:]
Any punctuation symbol
PT
Pass through
mod_rewrite Server Variables: Special
[:space:]
Any space character
R[=code]
Redirect to new URL, with optional
%{API_VERSION}
code (see below)
[:blank:]
Space or tab
%{THE_REQUEST}
QSA
Append query string
There's an excellent regular expression tester at:
%{REQUEST_URI}
S=x
Skip next x rules
%{REQUEST_FILENAME}
T=mime-
Set mime type
%{IS_SUBREQ}
mod_rewrite Server Variables: HTTP Headers
type
%{HTTPS}
%{HTTP_USER_AGENT}
mod_rewrite RewriteCond Flags
%{HTTP_REFERER}
mod_rewrite Server Variables: Request
NC
Case insensitive
%{HTTP_COOKIE}
%{REMOTE_ADDR}
OR
Combine with next rule using 'OR' instead of
%{HTTP_FORWARDED}
%{REMOTE_HOST}
the default of 'AND'
%{HTTP_HOST}
%{REMOTE_PORT}
%{HTTP_PROXY_CONNECTION}
Redirection Header Codes
%{REMOTE_USER}
%{HTTP_ACCEPT}
301
Moved permanently
%{REMOTE_IDENT}
302
Moved temporarily (default)
%{REQUEST_METHOD}
mod_rewrite Server Variables: Server Internals
%{SCRIPT_FILENAME}
%{DOCUMENT_ROOT}
mod_rewrite Directives
%{PATH_INFO}
%{SERVER_ADMIN}
RewriteEngine
%{QUERY_STRING}
%{SERVER_NAME}
RewriteOptions
%{AUTH_TYPE}
%{SERVER_ADDR}
RewriteLog
%{SERVER_PORT}
RewriteLogLevel
mod_rewrite Server Variables: Time
%{SERVER_PROTOCOL}
RewriteLock
%{TIME_YEAR}
%{SERVER_SOFTWARE}
RewriteMap
%{TIME_MON}
RewriteBase
%{TIME_DAY}
RewriteCond
%{TIME_HOUR}
RewriteRule
%{TIME_MIN}
%{TIME_SEC}
%{TIME_WDAY}
%{TIME}

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2