intro.html.en revision 51151813f03f5a17eddc5bb6be0747330b4515d2
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe This file is generated from xml source: DO NOT EDIT
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<title>Apache mod_rewrite Introduction - Apache HTTP Server</title>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<link href="/images/favicon.ico" rel="shortcut icon" /></head>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="/faq/">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<div class="up"><a href="./"><img title="<-" alt="<-" src="/images/left.gif" /></a></div>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.3</a> > <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Apache mod_rewrite Introduction</h1>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<p><span>Available Languages: </span><a href="/en/rewrite/intro.html" title="English"> en </a> |
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<a href="/fr/rewrite/intro.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a></p>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<p>This document supplements the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<a href="/mod/mod_rewrite.html">reference documentation</a>. It
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowedescribes the basic concepts necessary for use of
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>. Other documents go into greater detail,
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowebut this doc should help the beginner get their feet wet.
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#introduction">Introduction</a></li>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<li><img alt="" src="/images/down.gif" /> <a href="#regex">Regular Expressions</a></li>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<li><img alt="" src="/images/down.gif" /> <a href="#rewriterule">RewriteRule basics</a></li>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<li><img alt="" src="/images/down.gif" /> <a href="#flags">Rewrite Flags</a></li>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<li><img alt="" src="/images/down.gif" /> <a href="#rewritecond">Rewrite conditions</a></li>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<li><img alt="" src="/images/down.gif" /> <a href="#rewritemap">Rewrite maps</a></li>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<li><img alt="" src="/images/down.gif" /> <a href="#htaccess">.htaccess files</a></li>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe</ul><h3>See also</h3><ul class="seealso"><li><a href="/mod/mod_rewrite.html">Module documentation</a></li><li><a href="remapping.html">Redirection and remapping</a></li><li><a href="access.html">Controlling access</a></li><li><a href="advanced.html">Advanced techniques and tricks</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li></ul></div>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<h2><a name="introduction" id="introduction">Introduction</a></h2>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<p>The Apache module <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> is a very powerful and
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowesophisticated module which provides a way to do URL manipulations. With
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweit, you can do nearly all types of URL rewriting that you may need. It
56e85d89d42a6980f31b800266649efbed338da3wroweis, however, somewhat complex, and may be intimidating to the beginner.
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweThere is also a tendency to treat rewrite rules as magic incantation,
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweusing them without actually understanding what they do.</p>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<p>This document attempts to give sufficient background so that what
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowefollows is understood, rather than just copied blindly.
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<p>Remember that many common URL-manipulation tasks don't require the
dcf1a2f80f16265f00fe147de1dbd91af9c32a42wrowefull power and complexity of <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>. For simple
3082ab8b90e027fa27680f140987ea7e20df5088wrowetasks, see <code class="module"><a href="/mod/mod_alias.html">mod_alias</a></code> and the documentation
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<p>Finally, before proceeding, be sure to configure
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowethe <code class="directive"><a href="/mod/mod_rewrite.html#rewritelog">RewriteLog</a></code>. Although
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowethis log file can give an overwhelming amount of information, it is
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweindispensable in debugging problems with <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweconfiguration, since it will tell you exactly how each rule is
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweprocessed.</p>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<p>The RewriteLog directive cannot be used in .htaccess files, but can
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweonly be set in the main server configuration file.</p>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<h2><a name="regex" id="regex">Regular Expressions</a></h2>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<p>mod_rewrite uses the <a href="http://pcre.org/">Perl Compatible
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweRegular Expression</a> vocabulary. In this document, we do not attempt
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweto provide a detailed reference to regular expressions. For that, we
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowerecommend the <a href="http://pcre.org/pcre.txt">PCRE man pages</a>, the
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<a href="http://perldoc.perl.org/perlre.html">Perl regular
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweexpression man page</a>, and <a href="http://www.oreilly.com/catalog/regex2/index.html">Mastering
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<p>In this document, we attempt to provide enough of a regex vocabulary
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweto get you started, without being overwhelming, in the hope that
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>s will be scientific
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweformulae, rather than magical incantations.</p>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<h3><a name="regexvocab" id="regexvocab">Regex vocabulary</a></h3>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<p>The following are the minimal building blocks you will need, in order
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweto write regular expressions and <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>s. They certainly do not
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowerepresent a complete regular expression vocabulary, but they are a good
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweplace to start, and should help you read basic regular expressions, as
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowewell as write your own.</p>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowecharacter</td><td><code>c.t</code> will match <code>cat</code>,
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<tr><td><code>+</code></td><td>Repeats the previous match one or more
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowetimes</td><td><code>a+</code> matches <code>a</code>, <code>aa</code>,
4e37bfe24d9ab0f4dc6e3a76c9ec2bc4231a61b8wrowe<tr><td><code>*</code></td><td>Repeats the previous match zero or more
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowetimes.</td><td><code>a*</code> matches all the same things
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<code>a+</code> matches, but will also match an empty string.</td></tr>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<tr><td><code>?</code></td><td>Makes the match optional.</td><td>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<code>colou?r</code> will match <code>color</code> and <code>colour</code>.</td>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<tr><td><code>^</code></td><td>Called an anchor, matches the beginning
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweof the string</td><td><code>^a</code> matches a string that begins with
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<tr><td><code>$</code></td><td>The other anchor, this matches the end of
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowethe string.</td><td><code>a$</code> matches a string that ends with
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<tr><td><code>( )</code></td><td>Groups several characters into a single
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweunit, and captures a match for use in a backreference.</td><td><code>(ab)+</code>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowematches <code>ababab</code> - that is, the <code>+</code> applies to the group.
14d5b1a7866541c4eb974f2d213d2aea59743c95wroweFor more on backreferences see <a href="#InternalBackRefs">below</a>.</td></tr>
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<tr><td><code>[ ]</code></td><td>A character class - matches one of the
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowecharacters</td><td><code>c[uoa]t</code> matches <code>cut</code>,
14d5b1a7866541c4eb974f2d213d2aea59743c95wrowe<tr><td><code>[^ ]</code></td><td>Negative character class - matches any character not specified</td><td><code>c[^/]t</code> matches <code>cat</code> or <code>c=t</code> but not <code>c/t</code></td></tr>
<p>In <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> the <code>!</code> character can be
<img src="/images/rewrite_rule_flow.png" alt="Flow of RewriteRule and RewriteCond matching" /><br />
<p>A <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> consists
like the <code class="directive"><a href="/mod/mod_alias.html#alias">Alias</a></code> directive.</p>
<p>The behavior of a <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> can be modified by the
<p>One or more <code class="directive"><a href="/mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>
following <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>. The
RewriteRule (.*) http://intranet.example.com$1
the <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to be
<p>Notice that the exclamation mark specifies a negative match, so the rule is only applied if the cookie does not contain "go".</p>
the <code class="directive"><a href="/mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>s can be
the <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> using the
<p>See <code class="directive"><a href="/mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>.</p>
setting (outside any <code class="directive"><a href="/mod/core.html#directory"><Directory></a></code> section) or
the <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>. In addition, the <code class="directive"><a href="/mod/mod_rewrite.html#rewritebase">RewriteBase</a></code> should be used to assure the request is properly mapped.</p>
<p><span>Available Languages: </span><a href="/en/rewrite/intro.html" title="English"> en </a> |
<a href="/fr/rewrite/intro.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a></p>
<p class="apache">Copyright 2009 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="/faq/">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p></div>