Show Menu

mod_rewrite Cheat Sheet by

A mod_rewrite Cheat Sheet - a quick reference guide for mod_rewrite, with rewrite flags, regular expression syntax and sample rules.
development     rewrite     mod-rewrite     apache     lamp     htaccess     linux     server     sysadmin

mod_re­write Rewrit­eRule Flags

C
Chained with next rule
CO=cookie
Set specified cookie
E=var:­value
Set enviro­nmental variable “var” to “value”
F
Forbidden (403 header)
G
Gone - no longer exists
H=handler
Set handler
L
Last - stop processing rules
N
Next - continue processing
NC
Case insens­itive
NE
Do not escape output
NS
Ignore if subrequest
P
Proxy
PT
Pass through
R[=code]
Redirect to new URL, with optional code (see below)
QSA
Append query string
S=x
Skip next x rules
T=mime­-type
Set mime type

mod_re­write Rewrit­eCond Flags

NC
Case insens­itive
OR
Combine with next rule using 'OR' instead of the default of 'AND'

Redire­ction Header Codes

301
Moved perman­ently
302
Moved tempor­arily (default)

mod_re­write Directives

Rewrit­eEngine
RewriteMap
Rewrit­eOp­tions
Rewrit­eBase
RewriteLog
Rewrit­eCond
Rewrit­eLo­gLevel
Rewrit­eRule
Rewrit­eLock
 

Regular Expres­sions Syntax

^
Start of string
$
End of string
.
Any single character
(a|b)
a or b
(...)
Group section
[abc]
In range (a, b or c)
[^abc]
Not in range
\s
White space
a?
Zero or one of a
a*
Zero or more of a
a*?
Zero or more, ungreedy
a+
One or more of a
a+?
One or more, ungreedy
a{3}
Exactly 3 of a
a{3,}
3 or more of a
a{,6}
Up to 6 of a
a{3,6}
3 to 6 of a
a{3,6}?
3 to 6 of a, ungreedy
\
Escape character
[:punct:]
Any punctu­ation symbol
[:space:]
Any space character
[:blank:]
Space or tab
There's an excellent regular expression tester at: http:/­/re­gex­pal.com/

mod_re­write Server Variables: HTTP Headers

%{HTTP­_US­ER_­AGENT}
%{HTTP­_RE­FERER}
%{HTTP­_CO­OKIE}
%{HTTP­_FO­RWA­RDED}
%{HTTP­_HOST}
%{HTTP­_PR­OXY­_CO­NNE­CTION}
%{HTTP­_AC­CEPT}

mod_re­write Server Variables: Server Internals

%{DOCU­MEN­T_ROOT}
%{SERV­ER_­ADMIN}
%{SERV­ER_­NAME}
%{SERV­ER_­ADDR}
%{SERV­ER_­PORT}
%{SERV­ER_­PRO­TOCOL}
%{SERV­ER_­SOF­TWARE}
 

mod_re­write Sample Rule: Site Moved

# Site moved perman­ently
Rewrit­eCond %{HTTP­_HOST} ^www.d­oma­in.com$ [NC]
Rewrit­eRule ^(.*)$ http:/­/ww­w.d­oma­in2.com/$1 [R=301,L]
Rewrites domain.com to domain­2.com

mod_re­write Sample Rule: Temporary Page Move

# Page has moved tempor­arily
Rewrit­eRule ^page.h­tml$ new_pa­ge.html [R,NC,L]
Rewrites domain.co­m/p­age.html to domain.co­m/n­ew_­pag­e.html

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}

Download the mod_rewrite Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/davechild_mod-rewrite.750.jpg

PDF (recommended)

Alternative Downloads

Thank The Author!

You can use Flattr to thank the author of this cheat sheet!

Like this cheat sheet? Check out our sponsors!

Readability-Score.com is a collection of tools to make your writing better. More readabile content means higher conversion rates and better reader engagement. Measure website and document readability, measure keyword density and more!

Click Here To Get Started!

 

Comments

Beau Garcia Beau Garcia, 10:01 23 Feb 12

Nice work Dave. One of the better references I've found.

Dave Dave, 08:52 20 May 12

I had to disable JavaScript to highlight the text due to the silly sliding tile thing.

John John, 08:52 20 May 12

I keep having to turn off JavaScript!!!!!!!!!!!!!!!!!!!!!

Guy Chapman Guy Chapman, 10:31 1 Jul 12

Nice. Concise and well presented. Thanks.

vaibhav jain vaibhav jain, 14:37 21 Dec 12

thanks sir . i am beginner in web developing. thanks for help . i will use your code for rewrite my query string

panzz panzz, 08:43 16 Feb 13

what is ungreedy mean ?

Marvin Giron Marvin Giron, 10:32 29 May 13

I have a question: ¿How can I write a rule what redirect from a URL to URL:PORT

Anuj Sethi Anuj Sethi, 09:34 31 Oct 14

Thanks man.. I got it. Great work done. Its a one stop shop for all rewrite formulas :)

rio rio, 11:37 25 Nov 14

I am trying to do a mod rewrite where it checks if the referring uri or clicked link contains a variable xyz and redirect it to another page. Or if referring uri/url has a variable ABC set, rest it to blank. Any ideas?

Billy Billy, 15:58 23 Mar 15

Good list - we also started using http:/htaccesscheatsheet.com/ (your list here has more directive/flags/server variables etc, but that website has some things you can just copy/paste in.

Billy Billy, 15:13 4 May 15

The link in my previous comment was a typo - it should be http://htaccesscheatsheet.com/ :)

Imran Imran, 12:41 20 Aug 15

Nice Style and well presented
Thanks !

temir temir, 10:59 7 Oct 15

what does exclamation mark (!) mean?

Namita Namita, 09:22 15 Feb 16

I have a Godaddy domain and I'm trying to enable cleaner urls so that a user can write www.abc.com/xyz instead of having to write www.abc.com/xyz.php but cannot get it to work. What am I doing wrong?

RewriteEngine On
Options +FollowSymLinks -MultiViews -Indexes
RewriteRule ^xyz$ xyz.php [NC,L]

TME520 TME520, 01:26 27 Apr 16

This cheat sheet has been very useful to me recently, thanks for the great work !

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Linux Command Line Cheat Sheet
          Subversion Cheat Sheet

          More Cheat Sheets by DaveChild

          Regular Expressions Cheat Sheet
          Linux Command Line Cheat Sheet
          CSS2 Cheat Sheet