Mod_rewrite Cheat Sheet

ADVERTISEMENT

mod_rewrite
mod_rewrite
Server Variables
Format
^
Start of string
R[=code]
Redirect to new URL, with
%{NAME_OF_VAR}
$
End of string
optional code (see below).
HTTP Headers
.
F
Any single character
Forbidden (sends 403 header)
HTTP_USER_AGENT
(a|b)
G
a or b
Gone (no longer exists)
HTTP_REFERER
(...)
P
HTTP_COOKIE
Group section
Proxy
HTTP_FORWARDED
[abc]
L
Item in range (a or b or c)
Last Rule
HTTP_HOST
[^abc]
N
Not in range (not a or b or c)
Next (i.e. restart rules)
HTTP_PROXY_CONNECTION
a?
C
Zero or one of a
Chain
HTTP_ACCEPT
a*
T=mime-type
Zero or more of a
Set Mime Type
Request
a+
NS
One or more of a
Skip if internal sub-request
REMOTE_ADDR
a{3}
NC
Exactly 3 of a
Case insensitive
REMOTE_HOST
a{3,}
3 or more of a
QSA
Append query string
REMOTE_USER
REMOTE_IDENT
a{3,6}
Between 3 and 6 of a
NE
Do not escape output
REQUEST_METHOD
!(pattern)
"Not" prefix. Apply rule when
PT
Pass through
SCRIPT_FILENAME
URL does not match pattern.
S=x
Skip next x rules
PATH_INFO
E=var:value
Set environmental variable
QUERY_STRING
AUTH_TYPE
"var" to "value".
Server
301
Moved permanently
DOCUMENT_ROOT
SERVER_ADMIN
302
Moved temporarily
SERVER_NAME
NC
Case insensitive
403
Forbidden
SERVER_ADDR
OR
Allows a rule to apply if one of
404
Not Found
SERVER_PORT
a series of conditions are true.
410
Gone
SERVER_PROTOCOL
SERVER_SOFTWARE
Time
TIME_YEAR
TIME_MON
# Site has permanently moved to new domain
TIME_DAY
#
to
TIME_HOUR
RewriteCond
%{HTTP_HOST}
^
[NC]
TIME_MIN
RewriteRule
^(.*)$
[R=301,L]
TIME_SEC
TIME_WDAY
TIME
# Page has moved temporarily
#
to
Special
RewriteRule
^page.htm$
new_page.htm
[R,NC,L]
API_VERSION
THE_REQUEST
REQUEST_URI
# Nice looking URLs (no querystring)
REQUEST_FILENAME
#
to
IS_SUBREQ
RewriteRule
^([A-Za-z0-9-]+)/?$
categories.php?name=$1
[L]
# Nice looking URLs (no querystring) with pagination
Directives
#
to
RewriteEngine
RewriteRule
^articles/([A-Za-z0-9-]+)/([0-9]+)/?$
article.php?name=$1&page=$2
[L]
RewriteOptions
RewriteLog
RewriteLogLevel
# Block referrer spam
RewriteLock
RewriteCond
%{HTTP_REFERER}
(weight)
[NC,OR]
RewriteMap
RewriteCond
%{HTTP_REFERER}
(drugs)
[NC]
RewriteBase
RewriteCond
RewriteRule
.*
-
[F]
RewriteRule
Available free from

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go