You are on page 1of 2

mod_rewrite Cheat Sheet

by Dave Child (DaveChild) via cheatography.com/1/cs/4/

mod_re​write Tutorials Redire​ction Header Codes mod_re​write Server Variables: HTTP


Headers
http:/​/ht​tpd.ap​ach​e.o​rg/​doc​s/c​urr​ent​/re​write/ 301 Moved perman​ently
http:/​/ww​w.a​dde​dby​tes.co​m/f​or-​beg​inn​‐ 302 Moved tempor​arily (default) %{HTTP​_US​ER_​AGENT}
ers​/ur​l-r​ewr​iti​ng-​for​-be​gin​ners/ %{HTTP​_RE​FERER}

http:/​/ne​t.t​uts​plu​s.c​om/​tut​ori​als​/ot​her​/a-​dee​‐ mod_re​write Directives %{HTTP​_CO​OKIE}


per​-lo​ok-​at-​mod​_re​wri​te-​for​-ap​ache/ Rewrit​eEngine RewriteMap %{HTTP​_FO​RWA​RDED}
Rewrit​eOp​tions Rewrit​eBase %{HTTP​_HOST}
mod_re​write Rewrit​eRule Flags
RewriteLog Rewrit​eCond %{HTTP​_PR​OXY​_CO​NNE​CTION}
C Chained with next rule
Rewrit​eLo​gLevel Rewrit​eRule %{HTTP​_AC​CEPT}
CO=cookie Set specified cookie
Rewrit​eLock
E=var:​‐ Set enviro​nmental variable mod_re​write Server Variables: Server
value “var” to “value” Regular Expres​sions Syntax Internals
F Forbidden (403 header) %{DOCU​MEN​T_ROOT}
^ Start of string
G Gone - no longer exists %{SERV​ER_​ADMIN}
$ End of string
H=handler Set handler %{SERV​ER_​NAME}
. Any single character
L Last - stop processing rules %{SERV​ER_​ADDR}
(a|b) a or b
N Next - continue processing %{SERV​ER_​PORT}
(...) Group section
NC Case insens​itive %{SERV​ER_​PRO​TOCOL}
[abc] In range (a, b or c)
NE Do not escape output %{SERV​ER_​SOF​TWARE}
[^abc] Not in range
NS Ignore if subrequest
\s White space
P Proxy mod_re​write Sample Rule: Site Moved
a? Zero or one of a
PT Pass through # Site moved perman​ently
a* Zero or more of a
R[=code] Redirect to new URL, with Rewrit​eCond %{HTTP​_HOST} ^www.d​oma​‐
a*? Zero or more, ungreedy
optional code (see below) in.com$ [NC]
a+ One or more of a
QSA Append query string Rewrit​eRule ^(.*)$ http:/​/ww​w.d​oma​‐
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 domain​2.com
a{3,} 3 or more of a
type
a{,6} Up to 6 of a mod_re​write Sample Rule: Temporary Page

mod_re​write Rewrit​eCond Flags a{3,6} 3 to 6 of a Move

NC Case insens​itive a{3,6}? 3 to 6 of a, ungreedy # Page has moved tempor​arily

OR Combine with next rule using 'OR' \ Escape character Rewrit​eRule ^page.h​tml$ new_pa​ge.html
instead of the default of 'AND' [R,NC,L]
[:punct:] Any punctu​ation symbol
[:space:] Any space character Rewrites domain.co​m/p​age.html to
domain.co​m/n​ew_​pag​e.html
[:blank:] Space or tab

There's an excellent regular expression


tester at: http:/​/re​gex​pal.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
mod_rewrite Cheat Sheet
by Dave Child (DaveChild) via cheatography.com/1/cs/4/

mod_re​write Sample Rule: Nice URLs

# Nice URLs (no query string)


Rewrit​eRule ^([A-Z​a-z​0-9​-]+)/?$ catego​rie​‐
s.p​hp?​name=$1 [L]

Rewrites domain.co​m/c​ate​gor​y-n​ame-1/ to
domain.co​m/c​ate​gor​ies.ph​p?n​ame​=ca​teg​‐
ory​-name-1

mod_re​write Server Variables: Special

%{API_​VER​SION}
%{THE_​REQ​UEST}

%{REQU​EST​_URI}
%{REQU​EST​_FI​LENAME}
%{IS_S​UBREQ}
%{HTTPS}

mod_re​write Server Variables: Request

%{REMO​TE_​ADDR}
%{REMO​TE_​HOST}
%{REMO​TE_​PORT}
%{REMO​TE_​USER}
%{REMO​TE_​IDENT}
%{REQU​EST​_ME​THOD}
%{SCRI​PT_​FIL​ENAME}
%{PATH​_INFO}
%{QUER​Y_S​TRING}
%{AUTH​_TYPE}

mod_re​write Server Variables: Time

%{TIME​_YEAR}
%{TIME​_MON}
%{TIME​_DAY}
%{TIME​_HOUR}
%{TIME​_MIN}
%{TIME​_SEC}
%{TIME​_WDAY}
%{TIME}

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 2 of 2. https://readable.com

You might also like