From: Daniel Gruno
Date: Thu, 19 Jul 2012 15:07:15 +0000 (+0000)
Subject: Backport some changes from 2.4 to 2.2, as they are also valid here.
X-Git-Tag: 2.2.23~119
X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7a4acf64d1fd434299186b6dac09f5a5b29aa8cc;p=thirdparty%2Fapache%2Fhttpd.git
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
---
diff --git a/docs/manual/rewrite/advanced.xml b/docs/manual/rewrite/advanced.xml
index 22da720efce..4d3b2983efc 100644
--- a/docs/manual/rewrite/advanced.xml
+++ b/docs/manual/rewrite/advanced.xml
@@ -27,8 +27,8 @@
-This document supplements the mod_rewrite
-reference documentation. It provides
+
This document supplements the mod_rewrite
+reference documentation. It provides
a few advanced techniques and tricks using mod_rewrite.
Note that many of these examples won't work unchanged in your
@@ -55,8 +55,8 @@ configuration.
Description:
- A common technique for distributing the burden of
- server load or storage space is called "sharding".
+
A common technique for distributing the burden of
+ server load or storage space is called "sharding".
When using this method, a front-end server will use the
url to consistently "shard" users or objects to separate
backend servers.
@@ -102,6 +102,9 @@ RewriteRule ^/u/([^/]+)/?(.*) http://${users-to-hos
+ See the RewriteMap
+ documentation for more discussion of the syntax of this directive.
+
@@ -124,22 +127,24 @@ RewriteRule ^/u/([^/]+)/?(.*) http://${users-to-hos
This is done via the following ruleset:
-
-# This example is valid in per-directory context only
-RewriteCond %{REQUEST_FILENAME} !-s
-RewriteRule ^page\.html$ page.cgi [T=application/x-httpd-cgi,L]
-
-
- Here a request for page.html
leads to an
- internal run of a corresponding page.cgi
if
- page.html
is missing or has filesize
- null. The trick here is that page.cgi
is a
- CGI script which (additionally to its STDOUT
)
- writes its output to the file page.html
.
- Once it has completed, the server sends out
- page.html
. When the webmaster wants to force
- a refresh of the contents, he just removes
- page.html
(typically from cron
).
+
+# This example is valid in per-directory context only
+RewriteCond %{REQUEST_URI} !-U
+RewriteRule ^(.+)\.html$ /regenerate_page.cgi [PT,L]
+
+
+ The -U
operator determines whether the test string
+ (in this case, REQUEST_URI
) 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 /regenerate_page.cgi
, 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.
+
+ 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.
@@ -247,61 +252,61 @@ RewriteRule ^(/[uge]/[^/]+/?.*):refresh /internal/cgi/apache/nph-refresh?f=$1
$| = 1;
# split the QUERY_STRING variable
-@pairs = split(/&/, $ENV{'QUERY_STRING'});
+@pairs = split( /&/, $ENV{'QUERY_STRING'} );
foreach $pair (@pairs) {
-($name, $value) = split(/=/, $pair);
-$name =~ tr/A-Z/a-z/;
-$name = 'QS_' . $name;
-$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
-eval "\$$name = \"$value\"";
+ ( $name, $value ) = split( /=/, $pair );
+ $name =~ tr/A-Z/a-z/;
+ $name = 'QS_' . $name;
+ $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
+ eval "\$$name = \"$value\"";
}
-$QS_s = 1 if ($QS_s eq '');
-$QS_n = 3600 if ($QS_n eq '');
-if ($QS_f eq '') {
-print "HTTP/1.0 200 OK\n";
-print "Content-type: text/html\n\n";
+$QS_s = 1 if ( $QS_s eq '' );
+$QS_n = 3600 if ( $QS_n eq '' );
+if ( $QS_f eq '' ) {
+ print "HTTP/1.0 200 OK\n";
+ print "Content-type: text/html\n\n";
print "<b>ERROR</b>: No file given\n";
-exit(0);
+ exit(0);
}
-if (! -f $QS_f) {
-print "HTTP/1.0 200 OK\n";
-print "Content-type: text/html\n\n";
+if ( !-f $QS_f ) {
+ print "HTTP/1.0 200 OK\n";
+ print "Content-type: text/html\n\n";
print "<b>ERROR</b>: File $QS_f not found\n";
-exit(0);
+ exit(0);
}
sub print_http_headers_multipart_begin {
-print "HTTP/1.0 200 OK\n";
-$bound = "ThisRandomString12345";
-print "Content-type: multipart/x-mixed-replace;boundary=$bound\n";
-&print_http_headers_multipart_next;
+ print "HTTP/1.0 200 OK\n";
+ $bound = "ThisRandomString12345";
+ print "Content-type: multipart/x-mixed-replace;boundary=$bound\n";
+ &print_http_headers_multipart_next;
}
sub print_http_headers_multipart_next {
-print "\n--$bound\n";
+ print "\n--$bound\n";
}
sub print_http_headers_multipart_end {
-print "\n--$bound--\n";
+ print "\n--$bound--\n";
}
sub displayhtml {
-local($buffer) = @_;
-$len = length($buffer);
-print "Content-type: text/html\n";
-print "Content-length: $len\n\n";
-print $buffer;
+ local ($buffer) = @_;
+ $len = length($buffer);
+ print "Content-type: text/html\n";
+ print "Content-length: $len\n\n";
+ print $buffer;
}
sub readfile {
-local($file) = @_;
-local(*FP, $size, $buffer, $bytes);
-($x, $x, $x, $x, $x, $x, $x, $size) = stat($file);
-$size = sprintf("%d", $size);
+ local ($file) = @_;
+ local ( *FP, $size, $buffer, $bytes );
+ ( $x, $x, $x, $x, $x, $x, $x, $size ) = stat($file);
+ $size = sprintf( "%d", $size );
open(FP, "<$file");
-$bytes = sysread(FP, $buffer, $size);
-close(FP);
-return $buffer;
+ $bytes = sysread( FP, $buffer, $size );
+ close(FP);
+ return $buffer;
}
$buffer = &readfile($QS_f);
@@ -309,30 +314,30 @@ $buffer = &readfile($QS_f);
&displayhtml($buffer);
sub mystat {
-local($file) = $_[0];
-local($time);
+ local ($file) = $_[0];
+ local ($time);
-($x, $x, $x, $x, $x, $x, $x, $x, $x, $mtime) = stat($file);
-return $mtime;
+ ( $x, $x, $x, $x, $x, $x, $x, $x, $x, $mtime ) = stat($file);
+ return $mtime;
}
$mtimeL = &mystat($QS_f);
-$mtime = $mtime;
-for ($n = 0; $n < $QS_n; $n++) {
-while (1) {
- $mtime = &mystat($QS_f);
- if ($mtime ne $mtimeL) {
- $mtimeL = $mtime;
- sleep(2);
- $buffer = &readfile($QS_f);
- &print_http_headers_multipart_next;
- &displayhtml($buffer);
- sleep(5);
- $mtimeL = &mystat($QS_f);
- last;
+$mtime = $mtime;
+for ( $n = 0 ; $n & lt ; $QS_n ; $n++ ) {
+ while (1) {
+ $mtime = &mystat($QS_f);
+ if ( $mtime ne $mtimeL ) {
+ $mtimeL = $mtime;
+ sleep(2);
+ $buffer = &readfile($QS_f);
+ &print_http_headers_multipart_next;
+ &displayhtml($buffer);
+ sleep(5);
+ $mtimeL = &mystat($QS_f);
+ last;
+ }
+ sleep($QS_s);
}
- sleep($QS_s);
-}
}
&print_http_headers_multipart_end;
@@ -401,7 +406,7 @@ RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /home/$2
Discussion:
- This technique will of course also work with other
+ This technique will of course also work with other
special characters that mod_rewrite, by default, URL-encodes.
@@ -490,4 +495,4 @@ RewriteCond %{ENV:rewritten} =1
-
+
diff --git a/docs/manual/rewrite/flags.xml b/docs/manual/rewrite/flags.xml
index b727ad7e931..1eb65bdd605 100644
--- a/docs/manual/rewrite/flags.xml
+++ b/docs/manual/rewrite/flags.xml
@@ -38,7 +38,7 @@ providing detailed explanations and examples.
Virtual hosts
Proxying
Using RewriteMap
-Advanced techniques and tricks
+Advanced techniques
When not to use mod_rewrite
Introduction
@@ -54,7 +54,7 @@ RewriteRule pattern target [Flag1,Flag2,Flag3]
a longer form, such as cookie
. Some flags take one or more
arguments. Flags are not case sensitive.
-Each flag (with a few exceptions)
+
Each flag (with a few exceptions)
has a long and short form. While it is most common to use
the short form, it is recommended that you familiarize yourself with the
long form, so that you remember what each flag is supposed to do.
@@ -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:
-
-RewriteRule ^(/.*)$ /index.php?show=$1
-
+RewriteRule ^search/(.*)$ /search.php?term=$1
+
+Given a search term of 'x & 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 & y/z', which
+isn't a valid URL, and so would be encoded as
+search.php?term=x%20&y%2Fz=
, which is not what was intended.
-This will map /C++
to
-/index.php?show=/C++
. But it will also map
-/C%2b%2b
to /index.php?show=/C++
, because
-the %2b
has been unescaped. With the B flag, it will
-instead map to /index.php?show=/C%2b%2b
.
+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
+/search.php?term=x%20%26%20y%2Fz
.
+
+Note that you may also need to set AllowEncodedSlashes to On
to get this
+particular example to work, as httpd does not allow encoded slashes in URLs, and
+returns a 404 if it sees one.
This escaping is particularly necessary in a proxy situation,
when the backend may break if presented with an unescaped URL.
@@ -132,7 +139,6 @@ security model.
You may optionally also set the following values:
-
- Lifetime
- The time for which the cookie will persist, in minutes.
- A value of 0 indicates that the cookie will persist only for the
@@ -336,13 +342,13 @@ immediately without considering further rules.
If you are using RewriteRule in either
-.htaccess
files or in
+.htaccess
files or in
Directory sections,
it is important to have some understanding of how the rules are
processed. The simplified form of this is that once the rules have been
processed, the rewritten request is handed back to the URL parsing
engine to do what it may with it. It is possible that as the rewritten
-request is handled, the .htaccess
file or
+request is handled, the .htaccess
file or
Directory section
may be encountered again, and thus the ruleset may be run again from the
start. Most commonly this will happen if one of the rules causes a
@@ -358,7 +364,7 @@ rules, as shown below.
The example given here will rewrite any request to
index.php
, giving the original request as a query string
argument to index.php
, however, the RewriteCond ensures that if the request
+module="mod_rewrite">RewriteCond ensures that if the request
is already for index.php
, the RewriteRule will be skipped.
@@ -440,7 +446,7 @@ On subrequests, it is not always useful, and can even cause errors, if
the complete set of rules are applied. Use this flag to exclude
problematic rules.
-To decide whether or not to use this rule: if you prefix URLs with
+
To decide whether or not to use this rule: if you prefix URLs with
CGI-scripts, to force them to be processed by the CGI-script, it's
likely that you will run into problems (or significant overhead)
on sub-requests. In these cases, use this flag.
@@ -499,17 +505,17 @@ use of the [PT] flag causes the result of the RewriteRule to be passed back through
URL mapping, so that location-based mappings, such as Alias, Redirect, or ScriptAlias, for example, might have a
+module="mod_alias">Redirect, or ScriptAlias, for example, might have a
chance to take effect.
-If, for example, you have an
+If, for example, you have an
Alias
for /icons, and have a RewriteRule pointing there, you should
-use the [PT] flag to ensure that the
+use the [PT] flag to ensure that the
Alias is evaluated.
@@ -567,8 +573,8 @@ will be used to generate the URL sent with the redirect.
-Any valid HTTP response status code may be specified,
-using the syntax [R=305], with a 302 status code being used by
+Any valid HTTP response status code may be specified,
+using the syntax [R=305], with a 302 status code being used by
default if none is specified. The status code specified need not
necessarily be a redirect (3xx) status code. However,
if a status code is outside the redirect range (300-399) then the
@@ -576,7 +582,7 @@ substitution string is dropped entirely, and rewriting is stopped as if
the L
were used.
In addition to response status codes, you may also specify redirect
-status using their symbolic names: temp
(default),
+status using their symbolic names: temp
(default),
permanent
, or seeother
.
@@ -590,22 +596,22 @@ URI in request' warnings.
S|skip
-The [S] flag is used to skip rules that you don't want to run. This
-can be thought of as a goto
statement in your rewrite
-ruleset. In the following example, we only want to run the RewriteRule if the requested URI
-doesn't correspond with an actual file.
-
-
-# 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
+The [S] flag is used to skip rules that you don't want to run. The
+syntax of the skip flag is [S=N], where N signifies
+the number of rules to skip. This can be thought of as a goto
+statement in your rewrite ruleset. In the following example, we only want
+to run the RewriteRule if the
+requested URI doesn't correspond with an actual file.
+
+
+# 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
-
+
This technique is useful because a RewriteCond only applies to the
@@ -625,14 +631,14 @@ RewriteRule .? - [S=3]
# IF the file exists, then:
- RewriteRule (.*\.gif) images.php?$1
- RewriteRule (.*\.html) docs.php?$1
- # Skip past the "else" stanza.
- RewriteRule .? - [S=1]
+ RewriteRule (.*\.gif) images.php?$1
+ RewriteRule (.*\.html) docs.php?$1
+ # Skip past the "else" stanza.
+ RewriteRule .? - [S=1]
# ELSE...
- RewriteRule (.*) 404.php?file=$1
+ RewriteRule (.*) 404.php?file=$1
# END
@@ -668,10 +674,10 @@ invariably be a less efficient solution than the alternatives.
If used in per-directory context, use only -
(dash)
-as the substitution for the entire round of mod_rewrite processing,
-otherwise the MIME-type set with this flag is lost due to an internal
+as the substitution for the entire round of mod_rewrite processing,
+otherwise the MIME-type set with this flag is lost due to an internal
re-processing (including subsequent rounds of mod_rewrite processing).
-The L
flag can be useful in this context to end the
+The L
flag can be useful in this context to end the
current round of mod_rewrite processing.
diff --git a/docs/manual/rewrite/intro.xml b/docs/manual/rewrite/intro.xml
index 428d5684074..6499bf1a261 100644
--- a/docs/manual/rewrite/intro.xml
+++ b/docs/manual/rewrite/intro.xml
@@ -41,7 +41,7 @@ but this doc should help the beginner get their feet wet.
Virtual hosts
Proxying
Using RewriteMap
-Advanced techniques and tricks
+Advanced techniques
When not to use mod_rewrite
Introduction
@@ -107,7 +107,7 @@ well as write your own.
characterc.t will match cat ,
cot , cut , etc. |
+ | Repeats the previous match one or more
-times | a+ matches a , aa ,
+times | a+ matches a , aa ,
aaa , etc |
* | Repeats the previous match zero or more
times. | a* matches all the same things
@@ -122,7 +122,7 @@ of the string | ^a matches a string that begins with
the string. | a$ matches a string that ends with
a . |
( ) | Groups several characters into a single
-unit, and captures a match for use in a backreference. | (ab)+
+unit, and captures a match for use in a backreference. | (ab)+
matches ababab - that is, the + applies to the group.
For more on backreferences see below. |
[ ] | A character class - matches one of the
@@ -145,15 +145,20 @@ the expression.
CondPattern, back-references are internally created
which can be used with the strings $N and
%N (see below). These are available for creating
- the strings Substitution and TestString.
- Figure 1 shows to which locations the back-references are
- transferred for expansion as well as illustrating the flow of the
- RewriteRule, RewriteCond matching.
+ the strings Substitution and TestString 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.
+
- 
- Figure 1: The back-reference flow through a rule.
+ Figure 1: The back-reference flow through a rule.
+ In this example, a request for /test/1234 would be transformed into /admin.foo?page=test&id=1234&host=admin.example.com .
@@ -168,13 +173,18 @@ of three arguments separated by spaces. The arguments are
[flags]: options affecting the rewritten request.
-The Pattern is always a regular
-expression 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).
+The Pattern is a regular expression.
+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.
+
- 
Figure 2: Syntax of the RewriteRule directive.
@@ -260,7 +270,7 @@ expression that must match the variable, and a third optional
argument is a list of flags that modify how the match is evaluated.
- 
Figure 3: Syntax of the RewriteCond directive
|