You are on page 1of 1

mod_rewrite

Regular Expressions Syntax Example: New domain

^ Start of string #domain.com to domain2.com


$ End of string RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
. Any single character RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]
(a|b) a or b
(...) Group section
Example: Page moved temporarily
[abc] Item in range (a, b or c)
[^abc] Not in range (not a, b or c) # domain.com/page.htm to domain.com/new_page.htm
\s White space RewriteRule ^page.htm$ new_page.htm [R,NC,L]
a? Zero or one of a
a* Zero or more of a
Example: Nice looking URLs (no querystring)
a*? Zero or more of a, ungreedy
a+ One or more of a # domain.com/category-name/ to domain.com/categories.php?name=category-name
a+? One or more of a, ungreedy RewriteRule ^([A-Za-z0-9-]+)/?$ categories.php?name=$1 [L]
a{3} Exactly 3 of a
a{3,} 3 or more of a
Example: Block Referrer Spam (where referring URL contains "viagra" or "xxx" )
a{3,6} 3 to 6 of a
a{3,6}? 3 to 6 of a, ungreedy RewriteCond %{HTTP_REFERER} (viagra) [NC,OR]
!(pattern) "Not" prefix. Apply rule if RewriteCond %{HTTP_REFERER} (xxx) [NC]
pattern does not match. RewriteRule .* - [F]

RewriteRule Flags Server Variables: HTTP Headers Server Variables: Time

C Chained with next rule %{HTTP_USER_AGENT} %{TIME_YEAR}


CO=cookie Set specified cookie %{HTTP_REFERER} %{TIME_MON}
E=var:value Set environmental variable %{HTTP_COOKIE} %{TIME_DAY}
“var” to “value” %{HTTP_FORWARDED} %{TIME_HOUR}
F Forbidden (403 header) %{HTTP_HOST} %{TIME_MIN}
G Gone - no longer exists %{HTTP_PROXY_CONNECTION} %{TIME_SEC}
H=handler Set handler %{HTTP_ACCEPT} %{TIME_WDAY}
L Last - stop processing rules %{TIME}
N Next - continue processing
Server Variables: Request
NC Case insensitive
Server Variables: Special
NE Do not escape output %{REMOTE_ADDR}
NS Ignore if subrequest %{REMOTE_HOST} %{API_VERSION}
P Proxy %{REMOTE_USER} %{THE_REQUEST}
PT Pass through %{REMOTE_IDENT} %{REQUEST_URI}
R[=code] Redirect to new URL, with %{REQUEST_METHOD} %{REQUEST_FILENAME}
optional code (see below) %{SCRIPT_FILENAME} %{IS_SUBREQ}
QSA Append query string %{PATH_INFO}
S=x Skip next x rules %{QUERY_STRING}
Directives
T=mime-type Set mime type %{AUTH_TYPE}
RewriteEngine
RewriteOptions
RewriteCond Flags Server Variables: HTTP Headers
RewriteLog
NC Case insensitive %{DOCUMENT_ROOT} RewriteLogLevel
OR Combines rules with logical %{SERVER_ADMIN} RewriteLock
"or" instead of "and". %{SERVER_NAME} RewriteMap
%{SERVER_ADDR} RewriteBase
%{SERVER_PORT} RewriteCond
Redirection Header Codes
%{SERVER_PROTOCOL} RewriteRule
301 Moved permanently %{SERVER_SOFTWARE}
302 Moved temporarily (default) Available free from
AddedBytes.com

You might also like