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

Chained with next rule
Set specified cookie
Set enviro­nmental variable “var” to “value”
Forbidden (403 header)
Gone - no longer exists
Set handler
Last - stop processing rules
Next - continue processing
Case insens­itive
Do not escape output
Ignore if subrequest
Pass through
Redirect to new URL, with optional code (see below)
Append query string
Skip next x rules
Set mime type

mod_re­write Rewrit­eCond Flags

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

Redire­ction Header Codes

Moved perman­ently
Moved tempor­arily (default)

mod_re­write Directives


Regular Expres­sions Syntax

Start of string
End of string
Any single character
a or b
Group section
In range (a, b or c)
Not in range
White space
Zero or one of a
Zero or more of a
Zero or more, ungreedy
One or more of a
One or more, ungreedy
Exactly 3 of a
3 or more of a
Up to 6 of a
3 to 6 of a
3 to 6 of a, ungreedy
Escape character
Any punctu­ation symbol
Any space character
Space or tab
There's an excellent regular expression tester at: http:/­/re­gex­

mod_re­write Server Variables: HTTP Headers


mod_re­write Server Variables: Server Internals


mod_re­write 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_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­m/p­age.html to­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­m/c­ate­gor­y-n­ame-1/ to­m/c­ate­gor­­p?n­ame­=ca­teg­ory­-name-1

mod_re­write Server Variables: Special


mod_re­write Server Variables: Request


mod_re­write Server Variables: Time


Download the mod_rewrite Cheat Sheet

2 Pages

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!



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:/ (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 :)

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 instead of having to write 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 !

ewiebenclif ewiebenclif, 21:15 22 Feb 19

<strong><a href="">excellence performance baselworld 2015 new wathces</a></strong>
<strong><a href="">Sale baselworld 2015 new wathces online</a></strong>
<strong><a href="">Discount baselworld 2015 new wathces</a></strong>

<title>Relojes Rolex réplica , Ofrecemos relojes falsos baratos, copiar relojes Rolex , réplica de relojes Rolex.</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Rolex Replica relojes, relojes Rolex falsos caliente de la venta en línea , los relojes Rolex falsos" />
<meta name="description" content="Fakerolexking que oferta la tienda en línea una variedad de Rolex Replica relojes , todos ellos son de alta calidad y bajo price.WelcometoRetailandWholesale para comprar, la imitación más profesionales de alta calidad relojes Rolex, relojes AAA Replica Rolex" />
<meta http-equiv="imagetoolbar" content="no" />

<base href="" />
<link rel="canonical" href="" />

<link rel="stylesheet" type="text/css" href="" />
<link rel="stylesheet" type="text/css" href="" />
<link rel="stylesheet" type="text/css" href="" />
<link rel="stylesheet" type="text/css" media="print" href="" />

<select name="currency" onchange="this.form.submit();">
<option value="USD">US Dollar</option>
<option value="EUR" selected="selected">Euro</option>
<option value="GBP">GB Pound</option>
<option value="CAD">Canadian Dollar</option>
<option value="AUD">Australian Dollar</option>

ewiebenclif ewiebenclif, 21:15 22 Feb 19

[b][url=]Sale cellini <strong><a href="">Sale cellini dual time watches</a></strong>
<strong><a href="">Fake cellini dual time mechanical watches</a></strong>

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
          MySQL Cheat Sheet

          More Cheat Sheets by DaveChild

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