From 824a8de0e01f3ef0b51705d323e4af74ea87a3c4 Mon Sep 17 00:00:00 2001 From: Daniel Gruno Date: Fri, 4 May 2012 14:25:32 +0000 Subject: [PATCH] Backporting syntax highlighting for rewrite/ git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x@1333985 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/rewrite/access.xml | 96 +++++------ docs/manual/rewrite/advanced.xml | 72 ++++---- docs/manual/rewrite/avoid.xml | 41 +++-- docs/manual/rewrite/flags.xml | 134 ++++++--------- docs/manual/rewrite/flags.xml.fr | 4 +- docs/manual/rewrite/index.xml.fr | 2 +- docs/manual/rewrite/index.xml.tr | 2 +- docs/manual/rewrite/index.xml.zh-cn | 2 +- docs/manual/rewrite/intro.xml | 53 +++--- docs/manual/rewrite/intro.xml.fr | 9 +- docs/manual/rewrite/proxy.xml | 20 +-- docs/manual/rewrite/remapping.xml | 248 +++++++++++++--------------- docs/manual/rewrite/rewritemap.xml | 99 ++++++----- docs/manual/rewrite/tech.xml.fr | 2 +- docs/manual/rewrite/vhosts.xml | 118 +++++++------ 15 files changed, 424 insertions(+), 478 deletions(-) diff --git a/docs/manual/rewrite/access.xml b/docs/manual/rewrite/access.xml index 08c2032eb2..2b6c677c5b 100644 --- a/docs/manual/rewrite/access.xml +++ b/docs/manual/rewrite/access.xml @@ -83,29 +83,29 @@ configuration. - -RewriteCond %{HTTP_REFERER} !^$
-RewriteCond %{HTTP_REFERER} !www.example.com [NC]
+ +RewriteCond %{HTTP_REFERER} !^$ +RewriteCond %{HTTP_REFERER} !www.example.com [NC] RewriteRule \.(gif|jpg|png)$ - [F,NC] -
+

In this second example, instead of failing the request, we display an alternate image instead.

- -RewriteCond %{HTTP_REFERER} !^$
-RewriteCond %{HTTP_REFERER} !www.example.com [NC]
+ +RewriteCond %{HTTP_REFERER} !^$ +RewriteCond %{HTTP_REFERER} !www.example.com [NC] RewriteRule \.(gif|jpg|png)$ /images/go-away.png [R,NC] -
+

In the third example, we redirect the request to an image on some other site.

- -RewriteCond %{HTTP_REFERER} !^$
-RewriteCond %{HTTP_REFERER} !www.example.com [NC]
+ +RewriteCond %{HTTP_REFERER} !^$ +RewriteCond %{HTTP_REFERER} !www.example.com [NC] RewriteRule \.(gif|jpg|png)$ http://other.example.com/image.gif [R,NC] -
+

Of these techniques, the last two tend to be the most effective in getting people to stop hotlinking your images, because they will @@ -120,14 +120,14 @@ RewriteRule \.(gif|jpg|png)$ http://other.example.com/image.gif than redirecting that request elsewhere, this can be accomplished without the use of mod_rewrite:

- - SetEnvIf Referer example\.com localreferer
- <FilesMatch \.(jpg|png|gif)$>
- Order deny,allow
- Deny from all
- Allow from env=localreferer
- </FilesMatch> -
+ +SetEnvIf Referer example\.com localreferer +<FilesMatch \.(jpg|png|gif)$> + Order deny,allow + Deny from all + Allow from env=localreferer +</FilesMatch> + @@ -170,11 +170,11 @@ RewriteRule \.(gif|jpg|png)$ http://other.example.com/image.gif range, if you are trying to block that user agent only from the particular source.

- -RewriteCond %{HTTP_USER_AGENT} ^NameOfBadRobot
-RewriteCond %{REMOTE_ADDR} =123\.45\.67\.[8-9]
+ +RewriteCond %{HTTP_USER_AGENT} ^NameOfBadRobot +RewriteCond %{REMOTE_ADDR} =123\.45\.67\.[8-9] RewriteRule ^/secret/files/ - [F] -
+
Discussion:
@@ -184,14 +184,14 @@ RewriteRule ^/secret/files/ - [F] Rather than using mod_rewrite for this, you can accomplish the same end using alternate means, as illustrated here:

- - SetEnvIfNoCase User-Agent ^NameOfBadRobot goaway
- <Location /secret/files>
- Order allow,deny
- Allow from all
- Deny from env=goaway
- </Location> -
+ +SetEnvIfNoCase User-Agent ^NameOfBadRobot goaway +<Location /secret/files> + Order allow,deny + Allow from all + Deny from env=goaway +</Location> +

As noted above, this technique is trivial to circumvent, by simply modifying the USER_AGENT request header. If you @@ -221,13 +221,13 @@ RewriteRule ^/secret/files/ - [F]

Solution:
- -RewriteEngine on
-RewriteMap hosts-deny txt:/path/to/hosts.deny
-RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND [OR]
-RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND
+ +RewriteEngine on +RewriteMap hosts-deny txt:/path/to/hosts.deny +RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND [OR] +RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND RewriteRule ^ - [F] -
+ ##
@@ -275,17 +275,17 @@ bsdti1.sdm.de -

The following ruleset uses a map file to associate each Referer with a redirection target.

- -RewriteMap deflector txt:/path/to/deflector.map
-
-RewriteCond %{HTTP_REFERER} !=""
-RewriteCond ${deflector:%{HTTP_REFERER}} =-
-RewriteRule ^ %{HTTP_REFERER} [R,L]
-
-RewriteCond %{HTTP_REFERER} !=""
-RewriteCond ${deflector:%{HTTP_REFERER}|NOT-FOUND} !=NOT-FOUND
+ +RewriteMap deflector txt:/path/to/deflector.map + +RewriteCond %{HTTP_REFERER} !="" +RewriteCond ${deflector:%{HTTP_REFERER}} =- +RewriteRule ^ %{HTTP_REFERER} [R,L] + +RewriteCond %{HTTP_REFERER} !="" +RewriteCond ${deflector:%{HTTP_REFERER}|NOT-FOUND} !=NOT-FOUND RewriteRule ^ ${deflector:%{HTTP_REFERER}} [R,L] -
+

The map file lists redirection targets for each referer, or, if we just wish to redirect back to where they came from, a "-" is diff --git a/docs/manual/rewrite/advanced.xml b/docs/manual/rewrite/advanced.xml index 8ddf03bc97..5464727361 100644 --- a/docs/manual/rewrite/advanced.xml +++ b/docs/manual/rewrite/advanced.xml @@ -92,13 +92,13 @@ http://physical_host_of_user1/u/user/anypath files assuming that server0 is a default server which will be used if a user has no entry in the map:

- -RewriteEngine on
-
-RewriteMap users-to-hosts txt:/path/to/map.users-to-hosts
-
+ +RewriteEngine on + +RewriteMap users-to-hosts txt:/path/to/map.users-to-hosts + RewriteRule ^/u/([^/]+)/?(.*) http://${users-to-hosts:$1|server0}/u/$1/$2 -
+
@@ -127,11 +127,11 @@ RewriteRule ^/u/([^/]+)/?(.*) http://${users-to-hos
This is done via the following ruleset: - -# This example is valid in per-directory context only
-RewriteCond %{REQUEST_URI} !-U
+ +# 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 @@ -169,12 +169,12 @@ RewriteRule ^(.+)\.html$ /regenerate_page.cgi [PT,L] module="mod_rewrite">RewriteMap and a list of servers to accomplish this.

- -RewriteEngine on
-RewriteMap lb rnd:/path/to/serverlist.txt
-
+ +RewriteEngine on +RewriteMap lb rnd:/path/to/serverlist.txt + RewriteRule ^/(.*) http://${lb:servers}/$1 [P,L] -
+

serverlist.txt will contain a list of the servers:

@@ -230,9 +230,9 @@ nph-refresh techniques. URL causes the 'page' to be refreshed every time it is updated on the filesystem.

- + RewriteRule ^(/[uge]/[^/]+/?.*):refresh /internal/cgi/apache/nph-refresh?f=$1 - +

Now when we reference the URL

@@ -250,7 +250,7 @@ RewriteRule ^(/[uge]/[^/]+/?.*):refresh /internal/cgi/apache/nph-refresh?f=$1 one would usually say "left as an exercise to the reader" ;-) I will provide this, too.

-
+
 #!/sw/bin/perl
 ##
 ##  nph-refresh -- NPH/CGI script for auto refreshing pages
@@ -272,13 +272,13 @@ $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 "&lt;b&gt;ERROR&lt;/b&gt;: No file given\n";
+print "<b>ERROR</b>: No file given\n";
 exit(0);
 }
 if (! -f $QS_f) {
 print "HTTP/1.0 200 OK\n";
 print "Content-type: text/html\n\n";
-print "&lt;b&gt;ERROR&lt;/b&gt;: File $QS_f not found\n";
+print "<b>ERROR</b>: File $QS_f not found\n";
 exit(0);
 }
 
@@ -310,7 +310,7 @@ local($file) = @_;
 local(*FP, $size, $buffer, $bytes);
 ($x, $x, $x, $x, $x, $x, $x, $size) = stat($file);
 $size = sprintf("%d", $size);
-open(FP, "&lt;$file");
+open(FP, "<$file");
 $bytes = sysread(FP, $buffer, $size);
 close(FP);
 return $buffer;
@@ -352,7 +352,7 @@ while (1) {
 exit(0);
 
 ##EOF##
-
+
@@ -381,10 +381,10 @@ exit(0);

We use the following ruleset to expand the tilde URLs into the above layout.

- -RewriteEngine on
+ +RewriteEngine on RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /home/$2/$1/public_html$3 -
+ @@ -440,13 +440,13 @@ RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /home/$2>STRING and =STRING we can do time-dependent redirects:

- -RewriteEngine on
-RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
-RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900
-RewriteRule ^foo\.html$ foo.day.html [L]
+ +RewriteEngine on +RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700 +RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900 +RewriteRule ^foo\.html$ foo.day.html [L] RewriteRule ^foo\.html$ foo.night.html -
+

This provides the content of foo.day.html under the URL foo.html from @@ -485,17 +485,17 @@ RewriteRule ^foo\.html$ foo.night.html

Use the [E] flag to set an environment variable.

- -RewriteEngine on
+ +RewriteEngine on RewriteRule ^/horse/(.*) /pony/$1 [E=rewritten:1] -
+

Later in your ruleset you might check for this environment variable using a RewriteCond:

- + RewriteCond %{ENV:rewritten} =1 - +

Note that environment variables do not survive an external redirect. You might consider using the [CO] flag to set a diff --git a/docs/manual/rewrite/avoid.xml b/docs/manual/rewrite/avoid.xml index 96d13c88d3..5a9b198daa 100644 --- a/docs/manual/rewrite/avoid.xml +++ b/docs/manual/rewrite/avoid.xml @@ -90,25 +90,22 @@ and /one/three/four.html.

To redirect URLs under /one to http://one.example.com, do the following:

- -Redirect /one/ http://one.example.com/ - +Redirect /one/ http://one.example.com/

To redirect http URLs to https, do the following:

- + <VirtualHost *:80> -ServerName www.example.com
-Redirect / https://www.example.com/
+ ServerName www.example.com + Redirect / https://www.example.com/ </VirtualHost > -
+ <VirtualHost *:443> -ServerName www.example.com
-
-# ... SSL configuration goes here
+ ServerName www.example.com + # ... SSL configuration goes here </VirtualHost > -
+

The use of RewriteRule to perform this task may be appropriate if there are other RewriteRule directives in @@ -133,7 +130,7 @@ is possible to perform this mapping with mod_rewrite, and performance.

Using Alias -Alias /cats /var/www/virtualhosts/felines/htdocs +Alias /cats /var/www/virtualhosts/felines/htdocs

@@ -175,18 +172,18 @@ seems like the right approach.

href="flags.html#flag_p">[P] flag to pass rewritten URIs through mod_proxy.

- + RewriteRule ^/?images(.*) http://imageserver.local/images$1 [P] - +

However, in many cases, when there is no actual pattern matching needed, as in the example shown above, the ProxyPass directive is a better choice. The example here could be rendered as:

- + ProxyPass /images/ http://imageserver.local/images/ - +

Note that whether you use RewriteRule or ProxyPass, you'll still need to use the ProxyPassReverse directive to catch redirects issued from the back-end server:

- + ProxyPassReverse /images/ http://imageserver.local/images/ - +

You may need to use RewriteRule instead when there are other RewriteRules in effect in the same scope, as a @@ -219,11 +216,11 @@ hostname, such as www.example.com instead of example.com. This can be done using the If directive, as shown here:

- -<If "$req{Host} != 'www.example.com'">
-RedirectMatch (.*) http://www.example.com$1
+ +<If "req('Host') != 'www.example.com'"> + Redirect / http://www.example.com/ </If> -
+

This technique can be used to take actions based on any request header, response header, or environment variable, replacing diff --git a/docs/manual/rewrite/flags.xml b/docs/manual/rewrite/flags.xml index a2f1f9d54e..64b78d423a 100644 --- a/docs/manual/rewrite/flags.xml +++ b/docs/manual/rewrite/flags.xml @@ -46,9 +46,7 @@ providing detailed explanations and examples.

its behavior modified by one or more flags. Flags are included in square brackets at the end of the rule, and multiple flags are separated by commas.

- -RewriteRule pattern target [Flag1,Flag2,Flag3] - +RewriteRule pattern target [Flag1,Flag2,Flag3]

The flags all have a short form, such as CO, as well as a longer form, such as cookie. Some flags take one or more @@ -79,9 +77,7 @@ 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 ^search/(.*)$ /search.php?term=$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 @@ -167,10 +163,10 @@ browsers that support this feature.

Several examples are offered here:

- -RewriteEngine On
+ +RewriteEngine On RewriteRule ^/index\.html - [CO=frontdoor:yes:.example.com:1440:/] -
+

In the example give, the rule doesn't rewrite the request. The "-" rewrite target tells mod_rewrite to pass the request @@ -255,17 +251,17 @@ value of '1' if the requested URI is an image file. Then, that environment variable is used to exclude those requests from the access log.

- -RewriteRule \.(png|gif|jpg) - [E=image:1]
+ +RewriteRule \.(png|gif|jpg)$ - [E=image:1] CustomLog logs/access_log combined env=!image -
+

Note that this same effect can be obtained using SetEnvIf. This technique is offered as an example, not as a recommendation.

-
END +
END

Using the [END] flag terminates not only the current round of rewrite processing (like [L]) but also prevents any subsequent rewrite processing from occurring in per-directory (htaccess) context.

@@ -283,9 +279,7 @@ allows more flexibility in assigning a Forbidden status.

The following rule will forbid .exe files from being downloaded from your server.

- -RewriteRule \.exe - [F] - +RewriteRule \.exe - [F]

This example uses the "-" syntax for the rewrite target, which means that the requested URI is not modified. There's no reason to rewrite to @@ -304,9 +298,7 @@ longer available.

As with the [F] flag, you will typically use the "-" syntax for the rewrite target when using the [G] flag:

- -RewriteRule oldproduct - [G,NC] - +RewriteRule oldproduct - [G,NC]

When using [G], an [L] is implied - that is, the response is returned immediately, and no further rules are evaluated.

@@ -318,9 +310,7 @@ immediately, and no further rules are evaluated.

handler. For example, one might use this to force all files without a file extension to be parsed by the php handler:

- -RewriteRule !\. - [H=application/x-httpd-php] - +RewriteRule !\. - [H=application/x-httpd-php]

The regular expression above - !\. - will match any request @@ -332,9 +322,9 @@ For example, the following snippet used in per-server context allows .php files to be displayed by mod_php if they are requested with the .phps extension:

- + RewriteRule ^(/source/.+\.php)s$ $1 [H=application/x-httpd-php-source] - +

The regular expression above - ^(/source/.+\.php)s$ - will match any request that starts with /source/ followed by 1 or @@ -385,11 +375,11 @@ module="mod_rewrite">RewriteCond ensures that if the request is already for index.php, the RewriteRule will be skipped.

- -RewriteBase /
-RewriteCond %{REQUEST_URI} !=/index.php
+ +RewriteBase / +RewriteCond %{REQUEST_URI} !=/index.php RewriteRule ^(.*) /index.php?req=$1 [L,PT] -
+
N|next @@ -405,9 +395,7 @@ will replace A with B everywhere in a request, and will continue doing so until there are no more As to be replaced.

- -RewriteRule (.*)A(.*) $1B$2 [N] - +RewriteRule (.*)A(.*) $1B$2 [N]

You can think of this as a while loop: While this pattern still matches (i.e., while the URI still contains an @@ -427,9 +415,7 @@ to your dedicated image server. The match is case-insensitive, so that .jpg and .JPG files are both acceptable, for example.

- -RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC] - +RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC]
NE|noescape @@ -438,9 +424,7 @@ RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC] equivalent. Using the [NE] flag prevents that from happening.

- -RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R] - +RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]

The above example will redirect /anchor/xyz to @@ -481,9 +465,7 @@ requests. example, if you wanted all image requests to be handled by a back-end image server, you might do something like the following:

- -RewriteRule /(.*)\.(jpg|gif|png) http://images.example.com/$1.$2 [P] - +RewriteRule /(.*)\.(jpg|gif|png)$ http://images.example.com/$1.$2 [P]

Use of the [P] flag implies [L] - that is, the request is immediately pushed through the proxy, and any following rules will not be @@ -536,10 +518,10 @@ use the [PT] flag to ensure that the Alias is evaluated.

- -Alias /icons /usr/local/apache/icons
-RewriteRule /pics/(.+)\.jpg /icons/$1.gif [PT] -
+ +Alias /icons /usr/local/apache/icons +RewriteRule /pics/(.+)\.jpg$ /icons/$1.gif [PT] +

Omission of the [PT] flag in this case will cause the Alias to be @@ -568,9 +550,7 @@ Using the [QSA] flag causes the query strings to be combined.

Consider the following rule:

- -RewriteRule /pages/(.+) /page.php?page=$1 [QSA] - +RewriteRule /pages/(.+) /page.php?page=$1 [QSA]

With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] @@ -646,16 +626,16 @@ 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
+ +# 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 @@ -666,26 +646,22 @@ negate those conditions and add a RewriteRule with a [Skip] flag. Y use this to make pseudo if-then-else constructs: The last rule of the then-clause becomes skip=N, where N is the number of rules in the else-clause:

- -# Does the file exist?
-RewriteCond %{REQUEST_FILENAME} !-f
-RewriteCond %{REQUEST_FILENAME} !-d
-# Create an if-then-else construct by skipping 3 lines if we meant to go to the "else" stanza.
-RewriteRule .? - [S=3]
-
+ +# Does the file exist? +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +# Create an if-then-else construct by skipping 3 lines if we meant to go to the "else" stanza. +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 -
+

It is probably easier to accomplish this kind of configuration using the If, AddType directive.

For example, you might use the following technique to serve Perl source code as plain text, if requested in a particular way:

- -# Serve .pl files as plain text
+ +# Serve .pl files as plain text RewriteRule \.pl$ - [T=text/plain] -
+

Or, perhaps, if you have a camera that produces jpeg images without file extensions, you could force those images to be served with the correct MIME type by virtue of their file names:

- -# Files with 'IMG' in the name are jpg images.
+ +# Files with 'IMG' in the name are jpg images. RewriteRule IMG - [T=image/jpg] -
+

Please note that this is a trivial example, and could be better done using FilesMatch diff --git a/docs/manual/rewrite/flags.xml.fr b/docs/manual/rewrite/flags.xml.fr index 635e9b030e..caf50bbdb5 100644 --- a/docs/manual/rewrite/flags.xml.fr +++ b/docs/manual/rewrite/flags.xml.fr @@ -1,7 +1,7 @@ - + @@ -711,7 +711,7 @@ RewriteRule (.*\.html) docs.php?$1 qu'à la règle qui la suit immédiatement. Ainsi, si vous voulez qu'une directive RewriteCond s'applique à plusieurs règles RewriteRule, une technique possible consiste à inverser ces -conditions et à ajouter une RewriteRule avec le drapeau [Skip]. Cette technique permet +conditions et ajouter une RewriteRule avec le drapeau [Skip]. Cette technique permet d'élaborer des pseudo-constructions if-then-else : la dernière règle du bloc then contiendra skip=N, où N est le nombre de règles contenues dans le bloc else :

diff --git a/docs/manual/rewrite/index.xml.fr b/docs/manual/rewrite/index.xml.fr index 08d43a916b..13836fa98b 100644 --- a/docs/manual/rewrite/index.xml.fr +++ b/docs/manual/rewrite/index.xml.fr @@ -1,7 +1,7 @@ - + diff --git a/docs/manual/rewrite/index.xml.tr b/docs/manual/rewrite/index.xml.tr index 9a89faf77f..dd52f20fc4 100644 --- a/docs/manual/rewrite/index.xml.tr +++ b/docs/manual/rewrite/index.xml.tr @@ -1,7 +1,7 @@ - + + + @@ -178,8 +178,7 @@ arrières dans les expressions rationnelles effectuées par les règles RewriteRule et RewriteCond. Dans les chapitres suivants, nous examinerons comment utiliser ces références arrières, donc ne vous affolez pas si - elles vous paraissent un peu exotiques au premier abord. -

+ elles vous paraissent un peu exotiques au premier abord.

Le Modèle est toujours une expression rationnelle comparée au chemin de l'URL de la requête entrante (la partie située après le nom d'hôte mais avant tout point d'interrogation -qui indique le début d'une chaîne de requête).

+qui indique le début d'une chaîne de paramètres de +requête) ou, dans un contexte de répertoire, au chemin de la +requête relativement au répertoire pour lequel la règle est définie..

To simply map a URL to another server, we use the [P] flag, as follows:

- -RewriteEngine on
-RewriteBase /products/
-RewriteRule ^widget/(.*)$ http://product.example.com/widget/$1 [P]
+ +RewriteEngine on +RewriteBase /products/ +RewriteRule ^widget/(.*)$ http://product.example.com/widget/$1 [P] ProxyPassReverse /products/widget/ http://product.example.com/widget/ -
+

In the second example, we proxy the request only if we can't find the resource locally. This can be very useful when you're migrating from one server to another, and you're not sure if all the content has been migrated yet.

- -RewriteCond %{REQUEST_FILENAME} !-f
-RewriteCond %{REQUEST_FILENAME} !-d
-RewriteRule ^/(.*) http://old.example.com/$1 [P]
+ +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^/(.*) http://old.example.com/$1 [P] ProxyPassReverse / http://old.example.com/ -
+
Discussion:
diff --git a/docs/manual/rewrite/remapping.xml b/docs/manual/rewrite/remapping.xml index a5db6e8ff3..948efd587b 100644 --- a/docs/manual/rewrite/remapping.xml +++ b/docs/manual/rewrite/remapping.xml @@ -71,10 +71,10 @@ configuration.

We rewrite the old URL to the new one internally via the following rule:

- -RewriteEngine on
+ +RewriteEngine on RewriteRule ^/old\.html$ /new.html [PT] -
+ @@ -102,10 +102,10 @@ RewriteRule ^/old\.html$ /new.html [PT]

We force a HTTP redirect to the new URL which leads to a change of the browsers and thus the users view:

- -RewriteEngine on
+ +RewriteEngine on RewriteRule ^/foo\.html$ bar.html [R] -
+
Discussion
@@ -116,9 +116,7 @@ RewriteRule ^/foo\.html$ bar.html [ - - Redirect /foo.html /bar.html - + Redirect /foo.html /bar.html @@ -145,18 +143,18 @@ RewriteRule ^/foo\.html$ bar.html [ -With mod_rewrite -RewriteEngine on
+#With mod_rewrite +RewriteEngine on RewriteRule ^/docs/(.+) http://new.example.com/docs/$1 [R,L] -
+ -With RedirectMatch +#With RedirectMatch RedirectMatch ^/docs/(.*) http://new.example.com/docs/$1 - + -With Redirect +#With Redirect Redirect /docs/ http://new.example.com/docs/ - + @@ -186,11 +184,11 @@ Redirect /docs/ http://new.example.com/docs/ internally leads to the invocation of /~quux/foo.cgi.

- -RewriteEngine on
-RewriteBase /~quux/
+ +RewriteEngine on +RewriteBase /~quux/ RewriteRule ^foo\.html$ foo.cgi [H=cgi-script] -
+ @@ -217,21 +215,19 @@ RewriteRule ^foo\.html$ foo.cgi [H= - -# backward compatibility ruleset for
-# rewriting document.html to document.php
-# when and only when document.php exists
-<Directory /var/www/htdocs>
- -RewriteEngine on
-RewriteBase /var/www/htdocs
-
-RewriteCond $1.php -f
-RewriteCond $1.html !-f
-RewriteRule ^(.*).html$ $1.php
-
+ +# backward compatibility ruleset for +# rewriting document.html to document.php +# when and only when document.php exists +<Directory /var/www/htdocs> + RewriteEngine on + RewriteBase /var/www/htdocs + + RewriteCond $1.php -f + RewriteCond $1.html !-f + RewriteRule ^(.*).html$ $1.php </Directory> -
+
Discussion
@@ -279,64 +275,56 @@ but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).

- -<VirtualHost *:80>
- - ServerName undesired.example.com
- ServerAlias example.com notthis.example.com
-
- Redirect / http://www.example.com/
-
-</VirtualHost>
-
-<VirtualHost *:80>
- - ServerName www.example.com
-
+ +<VirtualHost *:80> + ServerName undesired.example.com + ServerAlias example.com notthis.example.com + + Redirect / http://www.example.com/ </VirtualHost> -
+ +<VirtualHost *:80> + ServerName www.example.com +</VirtualHost> +

You can alternatively accomplish this using the If directive:

- -<If "%{HTTP_HOST} != 'www.example.com'">
- -Redirect / http://www.example.com/ - + +<If "%{HTTP_HOST} != 'www.example.com'"> + Redirect / http://www.example.com/ </If> -
+

Or, for example, to redirect a portion of your site to HTTPS, you might do the following:

- -<If "%{SERVER_PROTOCOL} != 'HTTPS'">
- -Redirect /admin/ https://www.example.com/admin/ - + +<If "%{SERVER_PROTOCOL} != 'HTTPS'"> + Redirect /admin/ https://www.example.com/admin/ </If> -
+

If, for whatever reason, you still want to use mod_rewrite - if, for example, you need this to work with a larger set of RewriteRules - you might use one of the recipes below.

For sites running on a port other than 80:

- -RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
-RewriteCond %{HTTP_HOST} !^$
-RewriteCond %{SERVER_PORT} !^80$
+ +RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] +RewriteCond %{HTTP_HOST} !^$ +RewriteCond %{SERVER_PORT} !^80$ RewriteRule ^/?(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE] -
+

And for a site running on port 80

- -RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
-RewriteCond %{HTTP_HOST} !^$
+ +RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] +RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE] -
+

If you wanted to do this generically for all domain names - that @@ -345,11 +333,11 @@ RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE] example.com, you could use the following recipe:

- -RewriteCond %{HTTP_HOST} !^www\. [NC]
-RewriteCond %{HTTP_HOST} !^$
+ +RewriteCond %{HTTP_HOST} !^www\. [NC] +RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE] -
+

These rulesets will work either in your main server configuration file, or in a .htaccess file placed in the - -RewriteEngine on
-
-# first try to find it in dir1/...
-# ...and if found stop and be happy:
-RewriteCond %{DOCUMENT_ROOT}/dir1/%{REQUEST_URI} -f
-RewriteRule ^(.+) %{DOCUMENT_ROOT}/dir1/$1 [L]
-
-# second try to find it in dir2/...
-# ...and if found stop and be happy:
-RewriteCond %{DOCUMENT_ROOT}/dir2/%{REQUEST_URI} -f
-RewriteRule ^(.+) %{DOCUMENT_ROOT}/dir2/$1 [L]
-
-# else go on for other Alias or ScriptAlias directives,
-# etc.
+ +RewriteEngine on + +# first try to find it in dir1/... +# ...and if found stop and be happy: +RewriteCond %{DOCUMENT_ROOT}/dir1/%{REQUEST_URI} -f +RewriteRule ^(.+) %{DOCUMENT_ROOT}/dir1/$1 [L] + +# second try to find it in dir2/... +# ...and if found stop and be happy: +RewriteCond %{DOCUMENT_ROOT}/dir2/%{REQUEST_URI} -f +RewriteRule ^(.+) %{DOCUMENT_ROOT}/dir2/$1 [L] + +# else go on for other Alias or ScriptAlias directives, +# etc. RewriteRule ^ - [PT] -
+ @@ -424,13 +412,13 @@ RewriteRule ^ - [PT]

We'll use a RewriteMap directive to build a list of servers that we wish to use.

- -HostnameLookups on
-RewriteEngine on
-RewriteMap multiplex txt:/path/to/map.mirrors
-RewriteCond %{REMOTE_HOST} ([a-z]+)$ [NC]
+ +HostnameLookups on +RewriteEngine on +RewriteMap multiplex txt:/path/to/map.mirrors +RewriteCond %{REMOTE_HOST} ([a-z]+)$ [NC] RewriteRule ^/(.*)$ ${multiplex:%1|http://www.example.com/}$1 [R,L] -
+ ## map.mirrors -- Multiplexing Map
@@ -484,16 +472,16 @@ com http://www.example.com/
All other browsers receive page foo.32.html. This is done with the following ruleset:

- -RewriteCond %{HTTP_USER_AGENT} ^Mozilla/3.*
-RewriteRule ^foo\.html$ foo.NS.html [L]
-
-RewriteCond %{HTTP_USER_AGENT} ^Lynx/ [OR]
-RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12]
-RewriteRule ^foo\.html$ foo.20.html [L]
-
+ +RewriteCond %{HTTP_USER_AGENT} ^Mozilla/3.* +RewriteRule ^foo\.html$ foo.NS.html [L] + +RewriteCond %{HTTP_USER_AGENT} ^Lynx/ [OR] +RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12] +RewriteRule ^foo\.html$ foo.20.html [L] + RewriteRule ^foo\.html$ foo.32.html [L] -
+ @@ -524,9 +512,7 @@ RewriteRule ^foo\.html$ foo.32.html [L we replace /puppies and /canines by the canonical /dogs.

- -RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R] - +RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R]
Discussion:
@@ -534,9 +520,7 @@ RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R] This should really be accomplished with Redirect or RedirectMatch directives: - - RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2 - + RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2 @@ -565,17 +549,15 @@ using the following ruleset:

/about/:

- -RewriteEngine on
+ +RewriteEngine on RewriteRule ^/$ /about/ [R] -
+

Note that this can also be handled using the RedirectMatch directive:

- -RedirectMatch ^/$ http://example.com/about/ - +RedirectMatch ^/$ http://example.com/about/

Note also that the example rewrites only the root URL. That is, it rewrites a request for http://example.com/, but not a @@ -604,36 +586,30 @@ that should go to an existing resource such as an image, or a css file.

As of version 2.2.16, you should use the FallbackResource directive for this:

- -<Directory /var/www/my_blog>
- - FallbackResource index.php
-
+ +<Directory /var/www/my_blog> + FallbackResource index.php </Directory> -
+

However, in earlier versions of Apache, or if your needs are more complicated than this, you can use a variation of the following rewrite set to accomplish the same thing:

- -<Directory /var/www/my_blog>
- - RewriteBase /my_blog
-
- RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-f
- RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-d
- RewriteRule ^ index.php [PT]
-
+ +<Directory /var/www/my_blog> + RewriteBase /my_blog + + RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-f + RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-d + RewriteRule ^ index.php [PT] </Directory> -
+

If, on the other hand, you wish to pass the requested URI as a query string argument to index.php, you can replace that RewriteRule with:

- - RewriteRule (.*) index.php?$1 [PT,QSA] - +RewriteRule (.*) index.php?$1 [PT,QSA]

Note that these rulesets can be used in a .htaccess file, as well as in a <Directory> block.

diff --git a/docs/manual/rewrite/rewritemap.xml b/docs/manual/rewrite/rewritemap.xml index aa6386e257..79010f8026 100644 --- a/docs/manual/rewrite/rewritemap.xml +++ b/docs/manual/rewrite/rewritemap.xml @@ -60,9 +60,7 @@ configuration.

The syntax of the RewriteMap directive is as follows:

- -RewriteMap MapName MapType:MapSource - +RewriteMap MapName MapType:MapSource

The MapName is an arbitray name that you assign to the map, and which you will use in @@ -86,21 +84,15 @@ RewriteMap MapName MapType:MapSource

For example, you might define a RewriteMap as:

- - RewriteMap examplemap txt:/path/to/file/map.txt - + RewriteMap examplemap txt:/path/to/file/map.txt

You would then be able to use this map in a RewriteRule as follows:

- - RewriteRule ^/ex/(.*) ${examplemap:$1} - +RewriteRule ^/ex/(.*) ${examplemap:$1}

A default value can be specified in the event that nothing is found in the map:

- -RewriteRule ^/ex/(.*) ${examplemap:$1|/not_found.html} - +RewriteRule ^/ex/(.*) ${examplemap:$1|/not_found.html} Per-directory and .htaccess context

@@ -141,11 +133,11 @@ may be used, and give examples of each.

For example, we might use a mapfile to translate product names to product IDs for easier-to-remember URLs, using the following recipe:

- - Product to ID configuration - RewriteMap product2id txt:/etc/apache2/productmap.txt
- RewriteRule ^/product/(.*) /prods.php?id=${product2id:$1|NOTFOUND} [PT] -
+

Product to ID configuration

+ +RewriteMap product2id txt:/etc/apache2/productmap.txt +RewriteRule ^/product/(.*) /prods.php?id=${product2id:$1|NOTFOUND} [PT] +

We assume here that the prods.php script knows what to do when it received an argument of id=NOTFOUND when @@ -175,9 +167,7 @@ telephone 328 scope. If you're planning to use this in a .htaccess file, you'll need to remove the leading slash from the rewrite pattern in order for it to match anything: - - RewriteRule ^product/(.*) /prods.php?id=${product2id:$1|NOTFOUND} [PT] - + RewriteRule ^product/(.*) /prods.php?id=${product2id:$1|NOTFOUND} [PT] Cached lookups @@ -213,13 +203,13 @@ telephone 328 static www1|www2|www3|www4
dynamic www5|www6 +

Configuration directives

+ +RewriteMap servers rnd:/path/to/file/map.txt - Configuration directives - RewriteMap servers rnd:/path/to/file/map.txt
-
- RewriteRule ^/(.*\.(png|gif|jpg)) http://${servers:static}/$1 [NC,P,L]
- RewriteRule ^/(.*) http://${servers:dynamic}/$1 [P,L] -
+RewriteRule ^/(.*\.(png|gif|jpg)) http://${servers:static}/$1 [NC,P,L] +RewriteRule ^/(.*) http://${servers:dynamic}/$1 [P,L] +

So, when an image is requested and the first of these rules is matched, RewriteMap looks up the string @@ -250,9 +240,9 @@ static www1|www1|www2|www3|www4

You may optionally specify a particular dbm type:

- - RewriteMap examplemap dbm=sdbm:/etc/apache/mapfile.dbm - + +RewriteMap examplemap dbm=sdbm:/etc/apache/mapfile.dbm +

The type can be sdbm, gdbm, ndbm or db. However, it is recommended that you just use the You can then reference the resulting file in your RewriteMap directive:

- + RewriteMap mapname dbm:/etc/apache/mapfile.map - +

Note that with some dbm types, more than one file is generated, with @@ -322,10 +312,12 @@ by many requests. the int function, and then use that in your RewriteRule:

- Redirect a URI to an all-lowercase version of itself - RewriteMap lc int:tolower
- RewriteRule (.*?[A-Z]+.*) ${lc:$1} [R] -
+

Redirect a URI to an all-lowercase version of itself

+ + +RewriteMap lc int:tolower +RewriteRule (.*?[A-Z]+.*) ${lc:$1} [R] +

Please note that the example offered here is for @@ -365,22 +357,23 @@ by many requests.

A simple example is shown here which will replace all dashes with underscores in a request URI.

- - Rewrite configuration - RewriteMap d2u prg:/www/bin/dash2under.pl
- RewriteRule - ${d2u:%{REQUEST_URI}} -
- - dash2under.pl - #!/usr/bin/perl
- $| = 1; # Turn off I/O buffering
- while (<STDIN>) {
- - s/-/_/g; # Replace dashes with underscores
- print $_;
-
- }
-
+ +

Rewrite configuration

+ + +RewriteMap d2u prg:/www/bin/dash2under.pl
+RewriteRule - ${d2u:%{REQUEST_URI}} +
+ +

dash2under.pl

+ + #!/usr/bin/perl + $| = 1; # Turn off I/O buffering + while (<STDIN>) { + s/-/_/g; # Replace dashes with underscores + print $_; + } + Caution!
    @@ -423,9 +416,9 @@ this process, or if the script itself is very slow.

    If a query returns more than one row, a random row from the result set is used.

    - Example + Example RewriteMap myquery "fastdbd:SELECT destination FROM rewrite WHERE source = %s" - +
diff --git a/docs/manual/rewrite/tech.xml.fr b/docs/manual/rewrite/tech.xml.fr index 5d92b32da8..b87dfc28a6 100644 --- a/docs/manual/rewrite/tech.xml.fr +++ b/docs/manual/rewrite/tech.xml.fr @@ -1,7 +1,7 @@ - + diff --git a/docs/manual/rewrite/vhosts.xml b/docs/manual/rewrite/vhosts.xml index 1c4e05174f..f337263d07 100644 --- a/docs/manual/rewrite/vhosts.xml +++ b/docs/manual/rewrite/vhosts.xml @@ -71,14 +71,14 @@ mod_rewrite document.
- -RewriteEngine on
-
-RewriteMap lowercase int:tolower
-
-RewriteCond %{lowercase:%{HTTP_HOST}} ^www\.([^.]+)\.example\.com$
+ +RewriteEngine on + +RewriteMap lowercase int:tolower + +RewriteCond %{lowercase:%{HTTP_HOST}} ^www\.([^.]+)\.example\.com$ RewriteRule ^(.*) /home/%1/www$1 -
+
Discussion
@@ -129,39 +129,37 @@ dynamic content, and Alias resolution. any ScriptAlias directives, we must have mod_rewrite explicitly enact those mappings.

- -# get the server name from the Host: header
-UseCanonicalName Off
-
-# splittable logs
-LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
-CustomLog logs/access_log vcommon
-
-<Directory /www/hosts>
- - # ExecCGI is needed here because we can't force
- # CGI execution in the way that ScriptAlias does
- Options FollowSymLinks ExecCGI
-
-</Directory>
-
-RewriteEngine On
-
-# a ServerName derived from a Host: header may be any case at all
-RewriteMap lowercase int:tolower
-
-## deal with normal documents first:
-# allow Alias /icons/ to work - repeat for other aliases
-RewriteCond %{REQUEST_URI} !^/icons/
-# allow CGIs to work
-RewriteCond %{REQUEST_URI} !^/cgi-bin/
-# do the magic
-RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1
-
-## and now deal with CGIs - we have to force a handler
-RewriteCond %{REQUEST_URI} ^/cgi-bin/
-RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [H=cgi-script]
-
+ +# get the server name from the Host: header +UseCanonicalName Off + +# splittable logs +LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon +CustomLog logs/access_log vcommon + +<Directory /www/hosts> + # ExecCGI is needed here because we can't force + # CGI execution in the way that ScriptAlias does + Options FollowSymLinks ExecCGI +</Directory> + +RewriteEngine On + +# a ServerName derived from a Host: header may be any case at all +RewriteMap lowercase int:tolower + +## deal with normal documents first: +# allow Alias /icons/ to work - repeat for other aliases +RewriteCond %{REQUEST_URI} !^/icons/ +# allow CGIs to work +RewriteCond %{REQUEST_URI} !^/cgi-bin/ +# do the magic +RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1 + +## and now deal with CGIs - we have to force a handler +RewriteCond %{REQUEST_URI} ^/cgi-bin/ +RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [H=cgi-script] +
@@ -184,27 +182,27 @@ customer-N.example.com /www/customers/N

The httpd.conf should contain the following:

- -RewriteEngine on
-
-RewriteMap lowercase int:tolower
-
-# define the map file
-RewriteMap vhost txt:/www/conf/vhost.map
-
-# deal with aliases as above
-RewriteCond %{REQUEST_URI} !^/icons/
-RewriteCond %{REQUEST_URI} !^/cgi-bin/
-RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
-# this does the file-based remap
-RewriteCond ${vhost:%1} ^(/.*)$
-RewriteRule ^/(.*)$ %1/docs/$1
-
-RewriteCond %{REQUEST_URI} ^/cgi-bin/
-RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
-RewriteCond ${vhost:%1} ^(/.*)$
+ +RewriteEngine on + +RewriteMap lowercase int:tolower + +# define the map file +RewriteMap vhost txt:/www/conf/vhost.map + +# deal with aliases as above +RewriteCond %{REQUEST_URI} !^/icons/ +RewriteCond %{REQUEST_URI} !^/cgi-bin/ +RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$ +# this does the file-based remap +RewriteCond ${vhost:%1} ^(/.*)$ +RewriteRule ^/(.*)$ %1/docs/$1 + +RewriteCond %{REQUEST_URI} ^/cgi-bin/ +RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$ +RewriteCond ${vhost:%1} ^(/.*)$ RewriteRule ^/(.*)$ %1/cgi-bin/$1 [H=cgi-script] -
+
-- 2.40.0