From 461cc22127b34dde49e214f885c49cf32ddc22b0 Mon Sep 17 00:00:00 2001 From: Rich Bowen Date: Mon, 11 Feb 2008 16:17:19 +0000 Subject: [PATCH] More examples and a few clarifications where the text didn't match the example. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@620512 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/rewrite/flags.html.en | 53 +++++++++++++++++++++++++++---- docs/manual/rewrite/flags.xml | 53 +++++++++++++++++++++++++++---- 2 files changed, 92 insertions(+), 14 deletions(-) diff --git a/docs/manual/rewrite/flags.html.en b/docs/manual/rewrite/flags.html.en index d91ddac2a6..a8073eec0e 100644 --- a/docs/manual/rewrite/flags.html.en +++ b/docs/manual/rewrite/flags.html.en @@ -127,7 +127,8 @@ RewriteRule \.exe - [F]

This example uses the "-" syntax for the rewrite target, which means -that the requested URI is not modified.

+that the requested URI is not modified. There's no reason to rewrite to +another URI, if you're going to forbid the request.

@@ -136,6 +137,13 @@ that the requested URI is not modified.

response. This indicates that a resource used to be available, but is no 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] +

+

H|handler

Forces the resulting request to be handled with the specified @@ -174,7 +182,17 @@ start over.

It is therefore important, if you are using RewriteRule directives in one of these context that you take explicit steps to avoid rules looping, and not count solely on the [L] flag to terminate execution of a series of -rules.

+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, if the request is already +for index.php, this rull will be skipped.

+ +

+RewriteCond %{REQUEST_URI} !index\.php
+RewriteRule ^(.*) index.php?req=$1 [L] +

N|next

@@ -184,9 +202,18 @@ with extreme caution, as it may result in loop.

The [Next] flag could be used, for example, if you wished to replace a -certain string or letter repeatedly in a request. +certain string or letter repeatedly in a request. The example shown here +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] +

+ +

You can think of this as a while loop: While this +pattern still matches, perform this substitution.

+

NC|nocase

@@ -217,7 +244,8 @@ RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]

The above example will redirect /anchor/xyz to /bigpage.html#xyz. Omitting the [NE] will result in the # -being converted to its hexcode equivalent, %23. +being converted to its hexcode equivalent, %23, which will +then result in a 404 Not Found error condition.

@@ -271,7 +299,7 @@ use the [PT] flag to ensure that the

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

@@ -335,8 +363,10 @@ ruleset. In the following example, we only want to run the

+# 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
@@ -359,8 +389,17 @@ sent. This has the same effect as the

-# Files with 'IMG' in the name are gif images. -RewriteRule IMG - [T=image/gif] +# Serve .pl files as plan 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.
+RewriteRule IMG - [T=image/jpg]

Please note that this is a trivial example, and could be better done diff --git a/docs/manual/rewrite/flags.xml b/docs/manual/rewrite/flags.xml index 437120fbf4..6378b31570 100644 --- a/docs/manual/rewrite/flags.xml +++ b/docs/manual/rewrite/flags.xml @@ -135,7 +135,8 @@ RewriteRule \.exe - [F]

This example uses the "-" syntax for the rewrite target, which means -that the requested URI is not modified.

+that the requested URI is not modified. There's no reason to rewrite to +another URI, if you're going to forbid the request.

@@ -143,6 +144,13 @@ that the requested URI is not modified.

The [G] flag forces Apache to return a 410 Gone status with the response. This indicates that a resource used to be available, but is no 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] +
H|handler @@ -184,7 +192,17 @@ start over.

module="mod_rewrite">RewriteRule directives in one of these context that you take explicit steps to avoid rules looping, and not count solely on the [L] flag to terminate execution of a series of -rules.

+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, if the request is already +for index.php, this rull will be skipped.

+ + +RewriteCond %{REQUEST_URI} !index\.php
+RewriteRule ^(.*) index.php?req=$1 [L] +
N|next @@ -194,9 +212,18 @@ with extreme caution, as it may result in loop.

The [Next] flag could be used, for example, if you wished to replace a -certain string or letter repeatedly in a request. +certain string or letter repeatedly in a request. The example shown here +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] + + +

You can think of this as a while loop: While this +pattern still matches, perform this substitution.

+
NC|nocase @@ -228,7 +255,8 @@ RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]

The above example will redirect /anchor/xyz to /bigpage.html#xyz. Omitting the [NE] will result in the # -being converted to its hexcode equivalent, %23. +being converted to its hexcode equivalent, %23, which will +then result in a 404 Not Found error condition.

@@ -285,7 +313,7 @@ use the [PT] flag to ensure that the

-Alias /icons /usr/local/apache/icons +Alias /icons /usr/local/apache/icons
RewriteRule /pics/(.+)\.jpg /icons/$1.gif [PT]
@@ -350,8 +378,10 @@ module="mod_rewrite">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
@@ -376,8 +406,17 @@ module="mod_mime">AddType directive.

source code as plain text, if requested in a particular way:

-# Files with 'IMG' in the name are gif images. -RewriteRule IMG - [T=image/gif] +# Serve .pl files as plan 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.
+RewriteRule IMG - [T=image/jpg]

Please note that this is a trivial example, and could be better done -- 2.40.0