From 461cc22127b34dde49e214f885c49cf32ddc22b0 Mon Sep 17 00:00:00 2001
From: Rich Bowen
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]
+
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.
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]
+
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.
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.
-Alias /icons /usr/local/apache/icons
+Alias /icons /usr/local/apache/icons
RewriteRule /pics/(.+)\.jpg /icons/$1.gif [PT]
+# 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