Show Menu

mod_rewrite Cheat Sheet by DaveChild

A mod_rewrite Cheat Sheet - a quick reference guide for mod_rewrite, with rewrite flags, regular expression syntax and sample rules.

mod_rewrite RewriteRule Flags

CChained with next rule
CO=cookieSet specified cookie
E=var:­valueSet enviro­nmental variable “var” to “value”
FForbidden (403 header)
GGone - no longer exists
H=handlerSet handler
LLast - stop processing rules
NNext - continue processing
NCCase insens­itive
NEDo not escape output
NSIgnore if subrequest
PTPass through
R[=code]Redirect to new URL, with optional code (see below)
QSAAppend query string
S=xSkip next x rules
T=mime­-typeSet mime type

mod_rewrite RewriteCond Flags

NCCase insens­itive
ORCombine with next rule using 'OR' instead of the default of 'AND'

Redirection Header Codes

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

mod_rewrite Directives


Regular Expressions 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
\sWhite 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­

mod_rewrite Server Variables: HTTP Headers


mod_rewrite Server Variables: Server Internals


mod_rewrite Sample Rule: Site Moved

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

Rewrites to domain­

mod_rewrite Sample Rule: Temporary Page Move

# Page has moved tempor­arily
Rewrit­eRule ^page.h­tml$ new_pa­ge.html [R,NC,L]

Rewrites­m/p­age.html to­m/n­ew_­pag­e.html

mod_rewrite 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­m/c­ate­gor­y-n­ame-1/ to­m/c­ate­gor­­p?n­ame­=ca­teg­ory­-name-1

mod_rewrite Server Variables: Special


mod_rewrite Server Variables: Request


mod_rewrite Server Variables: Time


Share This Cheat Sheet!

Favourited by 27 Members:

sirgt ThibaultGomarin relizont Theosis SoulReverie lyroge Dranzz akramwaseem Epoc avalitan emre and 17 more ...


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

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.