From: Rich Bowen
-RewriteRule ^(/.*)$ /index.php?show=$1
+RewriteRule ^search/(.*)$ /search.php?term=$1
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.
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.
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.
diff --git a/docs/manual/rewrite/flags.xml b/docs/manual/rewrite/flags.xml index 58c5321cc17..b5438aa1a2f 100644 --- a/docs/manual/rewrite/flags.xml +++ b/docs/manual/rewrite/flags.xml @@ -80,14 +80,23 @@ Using the B flag, non-alphanumeric characters in backreferences will be escaped. For example, consider the rule: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.
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.
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 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.