diff options
Diffstat (limited to 'docs/manual/rewrite/rewrite_intro.html.en')
-rw-r--r-- | docs/manual/rewrite/rewrite_intro.html.en | 212 |
1 files changed, 164 insertions, 48 deletions
diff --git a/docs/manual/rewrite/rewrite_intro.html.en b/docs/manual/rewrite/rewrite_intro.html.en index e96dd216..e80d4441 100644 --- a/docs/manual/rewrite/rewrite_intro.html.en +++ b/docs/manual/rewrite/rewrite_intro.html.en @@ -14,9 +14,9 @@ <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> <p class="apache">Apache HTTP Server Version 2.2</p> <img alt="" src="../images/feather.gif" /></div> -<div class="up"><a href="./index.html"><img title="<-" alt="<-" src="../images/left.gif" /></a></div> +<div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div> <div id="path"> -<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.2</a></div><div id="page-content"><div id="preamble"><h1>Apache mod_rewrite Introduction</h1> +<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.2</a> > <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Apache mod_rewrite Introduction</h1> <div class="toplang"> <p><span>Available Languages: </span><a href="../en/rewrite/rewrite_intro.html" title="English"> en </a></p> </div> @@ -35,7 +35,6 @@ but this doc should help the beginner get their feet wet. <li><img alt="" src="../images/down.gif" /> <a href="#rewritecond">Rewrite conditions</a></li> <li><img alt="" src="../images/down.gif" /> <a href="#rewritemap">Rewrite maps</a></li> <li><img alt="" src="../images/down.gif" /> <a href="#htaccess">.htaccess files</a></li> -<li><img alt="" src="../images/down.gif" /> <a href="#EnvVar">Environment Variables</a></li> </ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module documentation</a></li><li><a href="rewrite_tech.html">Technical details</a></li><li><a href="rewrite_guide.html">Practical solutions to common problems</a></li><li><a href="rewrite_guide_advanced.html">Practical solutions to @@ -53,6 +52,19 @@ using them without actually understanding what they do.</p> <p>This document attempts to give sufficient background so that what follows is understood, rather than just copied blindly. </p> + +<p>Remember that many common URL-manipulation tasks don't require the +full power and complexity of <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>. For simple +tasks, see <code class="module"><a href="../mod/mod_alias.html">mod_alias</a></code> and the documentation +on <a href="../urlmapping.html">mapping URLs to the +filesystem</a>.</p> + +<p>Finally, before proceeding, be sure to configure +the <code class="directive"><a href="../mod/mod_rewrite.html#rewritelog">RewriteLog</a></code>. Although +this log file can give an overwhelming amount of information, it is +indispensable in debugging problems with <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> +configuration, since it will tell you exactly how each rule is +processed.</p> </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> <div class="section"> <h2><a name="regex" id="regex">Regular Expressions</a></h2> @@ -85,7 +97,7 @@ well as write your own.</p> <th>Example</th> </tr> -<tr><td><code>.</code></td><td>Matches any +<tr><td><code>.</code></td><td>Matches any single character</td><td><code>c.t</code> will match <code>cat</code>, <code>cot</code>, <code>cut</code>, etc.</td></tr> <tr><td><code>+</code></td><td>Repeats the previous match one or more @@ -94,10 +106,9 @@ times</td><td><code>a+</code> matches <code>a</code>, <code>aa</code>, <tr><td><code>*</code></td><td>Repeats the previous match zero or more times.</td><td><code>a*</code> matches all the same things <code>a+</code> matches, but will also match an empty string.</td></tr> -<tr><td><code>?</code></td><td>Makes the match optional.</td><td /></tr> -<tr><td><code>.</code></td><td>Matches any -character</td><td><code>colou?r</code> will match <code>color</code> and -<code>colour</code>.</td></tr> +<tr><td><code>?</code></td><td>Makes the match optional.</td><td> +<code>colou?r</code> will match <code>color</code> and <code>colour</code>.</td> +</tr> <tr><td><code>^</code></td><td>Called an anchor, matches the beginning of the string</td><td><code>^a</code> matches a string that begins with <code>a</code></td></tr> @@ -111,11 +122,14 @@ For more on backreferences see <a href="#InternalBackRefs">below</a>.</td></tr> <tr><td><code>[ ]</code></td><td>A character class - matches one of the characters</td><td><code>c[uoa]t</code> matches <code>cut</code>, <code>cot</code> or <code>cat</code>.</td></tr> -<tr><td><code>!</code></td><td>Not</td><td>Negates a match - that is, -ensures that it does not match.</td></tr> - +<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> </table> +<p>In <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> the <code>!</code> character can be +used before a regular expression to negate it. This is, a string will +be considered to have matched only if it does not match the rest of +the expression.</p> + <h3><a name="InternalBackRefs" id="InternalBackRefs">Regex Back-Reference Availability</a></h3> @@ -138,59 +152,161 @@ ensures that it does not match.</td></tr> </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> <div class="section"> <h2><a name="rewriterule" id="rewriterule">RewriteRule basics</a></h2> -<p> -Basic anatomy of a RewriteRule, with exhaustively annotated simple -examples. -</p> +<p>A <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> consists +of three arguments separated by spaces. The arguments are</p> +<ol> +<li><var>Pattern</var>: which incoming URLs should be affected by the rule;</li> +<li><var>Substitution</var>: where should the matching requests be sent;</li> +<li><var>[flags]</var>: options affecting the rewritten request.</li> +</ol> + +<p>The <var>Pattern</var> is always a <a href="#regex">regular +expression</a> matched against the URL-Path of the incoming request +(the part after the hostname but before any question mark indicating +the beginning of a query string).</p> + +<p>The <var>Substitution</var> can itself be one of three things:</p> + +<dl> +<dt>A full filesystem path to a resource</dt> +<dd> +<div class="example"><p><code> +RewriteRule ^/games.* /usr/local/games/web +</code></p></div> +<p>This maps a request to an arbitrary location on your filesystem, much +like the <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code> directive.</p> +</dd> + +<dt>A web-path to a resource</dt> +<dd> +<div class="example"><p><code> +RewriteRule ^/foo$ /bar +</code></p></div> +<p>If <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code> is set +to <code>/usr/local/apache2/htdocs</code>, then this directive would +map requests for <code>http://example.com/foo</code> to the +path <code>/usr/local/apache2/htdocs/bar</code>.</p> +</dd> + +<dt>An absolute URL</dt> +<dd> +<div class="example"><p><code> +RewriteRule ^/product/view$ http://site2.example.com/seeproduct.html [R] +</code></p></div> +<p>This tells the client to make a new request for the specified URL.</p> +</dd> +</dl> + +<p>The <var>Substitution</var> can also +contain <em>back-references</em> to parts of the incoming URL-path +matched by the <var>Pattern</var>. Consider the following:</p> +<div class="example"><p><code> +RewriteRule ^/product/(.*)/view$ /var/web/productdb/$1 +</code></p></div> +<p>The variable <code>$1</code> will be replaced with whatever text +was matched by the expression inside the parenthesis in +the <var>Pattern</var>. For example, a request +for <code>http://example.com/product/r14df/view</code> will be mapped +to the path <code>/var/web/productdb/r14df</code>.</p> + +<p>If there is more than one expression in parenthesis, they are +available in order in the +variables <code>$1</code>, <code>$2</code>, <code>$3</code>, and so +on.</p> + + </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> <div class="section"> <h2><a name="flags" id="flags">Rewrite Flags</a></h2> -<p>Discussion of the flags to RewriteRule, and when and why one might -use them.</p> +<p>The behavior of a <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> can be modified by the +application of one or more flags to the end of the rule. For example, the +matching behavior of a rule can be made case-insensitive by the +application of the <code>[NC]</code> flag: +</p> +<div class="example"><p><code> +RewriteRule ^puppy.html smalldog.html [NC] +</code></p></div> + +<p>For more details on the available flags, their meanings, and +examples, see the <a href="rewrite_flags.html">Rewrite Flags</a> document.</p> + </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> <div class="section"> <h2><a name="rewritecond" id="rewritecond">Rewrite conditions</a></h2> -<p>Discussion of RewriteCond, looping, and other related concepts. -</p> +<p>One or more <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> +directives can be used to restrict the types of requests that will be +subject to the +following <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>. The +first argument is a variable describing a characteristic of the +request, the second argument is a <a href="#regex">regular +expression</a> that must match the variable, and a third optional +argument is a list of flags that modify how the match is evaluated.</p> + +<p>For example, to send all requests from a particular IP range to a +different server, you could use:</p> +<div class="example"><p><code> +RewriteCond %{REMOTE_ADDR} ^10\.2\.<br /> +RewriteRule (.*) http://intranet.example.com$1 +</code></p></div> + +<p>When more than +one <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> is +specified, they must all match for +the <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to be +applied. For example, to deny requests that contain the word "hack" in +their query string, except if they also contain a cookie containing +the word "go", you could use:</p> +<div class="example"><p><code> +RewriteCond %{QUERY_STRING} hack<br /> +RewriteCond %{HTTP_COOKIE} !go<br /> +RewriteRule .* - [F] +</code></p></div> +<p>Notice that the exclamation mark specifies a negative match, so the rule is only applied if the cookie does not contain "go".</p> + +<p>Matches in the regular expressions contained in +the <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>s can be +used as part of the <var>Substitution</var> in +the <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> using the +variables <code>%1</code>, <code>%2</code>, etc. For example, this +will direct the request to a different directory depending on the +hostname used to access the site:</p> +<div class="example"><p><code> +RewriteCond %{HTTP_HOST} (.*)<br /> +RewriteRule ^/(.*) /sites/%1/$1 +</code></p></div> +<p>If the request was for <code>http://example.com/foo/bar</code>, +then <code>%1</code> would contain <code>example.com</code> +and <code>$1</code> would contain <code>foo/bar</code>.</p> + + + </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> <div class="section"> <h2><a name="rewritemap" id="rewritemap">Rewrite maps</a></h2> -<p>Discussion of RewriteMap, including simple, but heavily annotated, -examples.</p> +<p>See <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>.</p> </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> <div class="section"> <h2><a name="htaccess" id="htaccess">.htaccess files</a></h2> -<p>Discussion of the differences between rewrite rules in httpd.conf and -in .htaccess files.</p> -</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> -<div class="section"> -<h2><a name="EnvVar" id="EnvVar">Environment Variables</a></h2> - -<p>This module keeps track of two additional (non-standard) -CGI/SSI environment variables named <code>SCRIPT_URL</code> -and <code>SCRIPT_URI</code>. These contain the -<em>logical</em> Web-view to the current resource, while the -standard CGI/SSI variables <code>SCRIPT_NAME</code> and -<code>SCRIPT_FILENAME</code> contain the <em>physical</em> -System-view. </p> - -<p>Notice: These variables hold the URI/URL <em>as they were -initially requested</em>, <em>i.e.</em>, <em>before</em> any -rewriting. This is important because the rewriting process is -primarily used to rewrite logical URLs to physical -pathnames.</p> - -<div class="example"><h3>Example</h3><pre> -SCRIPT_NAME=/sw/lib/w3s/tree/global/u/rse/.www/index.html -SCRIPT_FILENAME=/u/rse/.www/index.html -SCRIPT_URL=/u/rse/ -SCRIPT_URI=http://en1.engelschall.com/u/rse/ -</pre></div> + +<p>Rewriting is typically configured in the main server configuration +setting (outside any <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code> section) or +inside <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> +containers. This is the easiest way to do rewriting and is +recommended. It is possible, however, to do rewriting +inside <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code> +sections or <a href="../howto/htaccess.html"><code>.htaccess</code> +files</a> at the expense of some additional complexity. This technique +is called per-directory rewrites.</p> + +<p>The main difference with per-server rewrites is that the path +prefix of the directory containing the <code>.htaccess</code> file is +stripped before matching in +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> </div></div> <div class="bottomlang"> <p><span>Available Languages: </span><a href="../en/rewrite/rewrite_intro.html" title="English"> en </a></p> </div><div id="footer"> -<p class="apache">Copyright 2007 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="apache">Copyright 2008 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> </body></html>
\ No newline at end of file |