Commit ea19e74f authored by Daniel Gruno's avatar Daniel Gruno
Browse files

Backport some changes from 2.4 to 2.2, as they are also valid here.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.2.x@1363364 13f79535-47bb-0310-9956-ffa450edef68
parent f85a6d8a
Loading
Loading
Loading
Loading
+80 −75
Original line number Diff line number Diff line
@@ -102,6 +102,9 @@ RewriteRule ^/u/<strong>([^/]+)</strong>/?(.*) http://<strong>${users-to-hos
    </dd>
  </dl>

  <p>See the <directive module="mod_rewrite">RewriteMap</directive>
  documentation for more discussion of the syntax of this directive.</p>

</section>

<section id="on-the-fly-content">
@@ -124,22 +127,24 @@ RewriteRule ^/u/<strong>([^/]+)</strong>/?(.*) http://<strong>${users-to-hos
    <dd>
      This is done via the following ruleset:

<example>
# This example is valid in per-directory context only<br />
RewriteCond %{REQUEST_FILENAME}   <strong>!-s</strong><br />
RewriteRule ^page\.<strong>html</strong>$          page.<strong>cgi</strong>   [T=application/x-httpd-cgi,L]
</example>

      <p>Here a request for <code>page.html</code> leads to an
      internal run of a corresponding <code>page.cgi</code> if
      <code>page.html</code> is missing or has filesize
      null. The trick here is that <code>page.cgi</code> is a
      CGI script which (additionally to its <code>STDOUT</code>)
      writes its output to the file <code>page.html</code>.
      Once it has completed, the server sends out
      <code>page.html</code>. When the webmaster wants to force
      a refresh of the contents, he just removes
      <code>page.html</code> (typically from <code>cron</code>).</p>
<highlight language="config">
# This example is valid in per-directory context only
RewriteCond %{REQUEST_URI}   !-U
RewriteRule ^(.+)\.html$          /regenerate_page.cgi   [PT,L]
</highlight>

    <p>The <code>-U</code> operator determines whether the test string
    (in this case, <code>REQUEST_URI</code>) is a valid URL. It does
    this via a subrequest. In the event that this subrequest fails -
    that is, the requested resource doesn't exist - this rule invokes
    the CGI program <code>/regenerate_page.cgi</code>, which generates
    the requested resource and saves it into the document directory, so
    that the next time it is requested, a static copy can be served.</p>

    <p>In this way, documents that are infrequently updated can be served in
    static form. if documents need to be refreshed, they can be deleted
    from the document directory, and they will then be regenerated the
    next time they are requested.</p>
    </dd>
  </dl>

+51 −45
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ providing detailed explanations and examples.</p>
<seealso><a href="vhosts.html">Virtual hosts</a></seealso>
<seealso><a href="proxy.html">Proxying</a></seealso>
<seealso><a href="rewritemap.html">Using RewriteMap</a></seealso>
<seealso><a href="advanced.html">Advanced techniques and tricks</a></seealso>
<seealso><a href="advanced.html">Advanced techniques</a></seealso>
<seealso><a href="avoid.html">When not to use mod_rewrite</a></seealso>

<section id="introduction"><title>Introduction</title>
@@ -79,15 +79,22 @@ so backreferences will be unescaped at the time they are applied.
Using the B flag, non-alphanumeric characters in backreferences
will be escaped. For example, consider the rule:</p>

<example>
RewriteRule ^(/.*)$ /index.php?show=$1
</example>
<highlight language="config">RewriteRule ^search/(.*)$ /search.php?term=$1</highlight>

<p>Given a search term of 'x &amp; y/z', a browser will encode it as
'x%20%26%20y%2Fz', making the request 'search/x%20%26%20y%2Fz'. Without the B
flag, this rewrite rule will map to 'search.php?term=x &amp; y/z', which
isn't a valid URL, and so would be encoded as
<code>search.php?term=x%20&amp;y%2Fz=</code>, which is not what was intended.</p>

<p>This will map <code>/C++</code> to
<code>/index.php?show=/C++</code>. But it will also map
<code>/C%2b%2b</code> to <code>/index.php?show=/C++</code>, because
the <code>%2b</code> has been unescaped.  With the B flag, it will
instead map to <code>/index.php?show=/C%2b%2b</code>.</p>
<p>With the B flag set on this same rule, the parameters are re-encoded
before being passed on to the output URL, resulting in a correct mapping to
<code>/search.php?term=x%20%26%20y%2Fz</code>.</p>

<p>Note that you may also need to set <directive
module="core">AllowEncodedSlashes</directive> to <code>On</code> to get this
particular example to work, as httpd does not allow encoded slashes in URLs, and
returns a 404 if it sees one.</p>

<p>This escaping is particularly necessary in a proxy situation,
when the backend may break if presented with an unescaped URL.</p>
@@ -132,7 +139,6 @@ security model.</dd>
<p>You may optionally also set the following values:</p>

<dl>

<dt>Lifetime</dt>
<dd>The time for which the cookie will persist, in minutes.</dd>
<dd>A value of 0 indicates that the cookie will persist only for the
@@ -590,22 +596,22 @@ URI in request' warnings.
</section>

<section id="flag_s"><title>S|skip</title>
<p>The [S] flag is used to skip rules that you don't want to run. This
can be thought of as a <code>goto</code> statement in your rewrite
ruleset. In the following example, we only want to run the <directive
module="mod_rewrite">RewriteRule</directive> if the requested URI
doesn't correspond with an actual file.</p>

<example>
# Is the request for a non-existent file?<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteCond %{REQUEST_FILENAME} !-d<br />
# If so, skip these two RewriteRules<br />
RewriteRule .? - [S=2]<br />
<br />
RewriteRule (.*\.gif) images.php?$1<br />
<p>The [S] flag is used to skip rules that you don't want to run. The 
syntax of the skip flag is [S=<em>N</em>], where <em>N</em> signifies 
the number of rules to skip. This can be thought of as a <code>goto</code> 
statement in your rewrite ruleset. In the following example, we only want 
to run the <directive module="mod_rewrite">RewriteRule</directive> if the 
requested URI doesn't correspond with an actual file.</p>

<highlight language="config">
# Is the request for a non-existent file?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If so, skip these two RewriteRules
RewriteRule .? - [S=2]
RewriteRule (.*\.gif) images.php?$1
RewriteRule (.*\.html) docs.php?$1
</example>
</highlight>

<p>This technique is useful because a <directive
module="mod_rewrite">RewriteCond</directive> only applies to the
+25 −15
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ but this doc should help the beginner get their feet wet.
<seealso><a href="vhosts.html">Virtual hosts</a></seealso>
<seealso><a href="proxy.html">Proxying</a></seealso>
<seealso><a href="rewritemap.html">Using RewriteMap</a></seealso>
<seealso><a href="advanced.html">Advanced techniques and tricks</a></seealso>
<seealso><a href="advanced.html">Advanced techniques</a></seealso>
<seealso><a href="avoid.html">When not to use mod_rewrite</a></seealso>

<section id="introduction"><title>Introduction</title>
@@ -145,15 +145,20 @@ the expression.</p>
      <em>CondPattern</em>, back-references are internally created
      which can be used with the strings <code>$N</code> and
      <code>%N</code> (see below). These are available for creating
      the strings <em>Substitution</em> and <em>TestString</em>.
      Figure 1 shows to which locations the back-references are
      transferred for expansion as well as illustrating the flow of the
      RewriteRule, RewriteCond matching.</p>
      the strings <em>Substitution</em> and <em>TestString</em> as 
      outlined in the following chapters. Figure 1 shows to which 
      locations the back-references are transferred for expansion as 
      well as illustrating the flow of the RewriteRule, RewriteCond 
      matching. In the next chapters, we will be exploring how to use 
      these back-references, so do not fret if it seems a bit alien 
      to you at first.
      </p>

<p class="figure">
      <img src="../images/rewrite_rule_flow.png" 
      <img src="../images/rewrite_backreferences.png"
      alt="Flow of RewriteRule and RewriteCond matching" /><br />
      <dfn>Figure 1:</dfn> The back-reference flow through a rule.
      <dfn>Figure 1:</dfn> The back-reference flow through a rule.<br />
      In this example, a request for <code>/test/1234</code> would be transformed into <code>/admin.foo?page=test&amp;id=1234&amp;host=admin.example.com</code>.
</p>

</section>
@@ -168,10 +173,15 @@ of three arguments separated by spaces. The arguments are</p>
<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>Pattern</var> is a <a href="#regex">regular expression</a>. 
It is initially (for the first rewrite rule or until a substitution occurs) 
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) or, in per-directory context, against the request's path relative 
to the directory for which the rule is defined. Once a substitution has 
occured, the rules that follow are matched against the substituted
value.
</p>

<p class="figure">
      <img src="../images/syntax_rewriterule.png"