From: Daniel Gruno In this second example, instead of failing the request, we display
an alternate image instead. In the third example, we redirect the request to an image on some
other site. 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:
-RewriteCond %{HTTP_REFERER} !www.example.com [NC]
+
-RewriteCond %{HTTP_REFERER} !www.example.com [NC]
+
-RewriteCond %{HTTP_REFERER} !www.example.com [NC]
+
- <FilesMatch \.(jpg|png|gif)$>
- Order deny,allow
- Deny from all
- Allow from env=localreferer
- </FilesMatch>
-
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]
The following ruleset uses a map file to associate each Referer with a redirection target.
-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:
-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.
serverlist.txt
will contain a list of the servers:
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 "<b>ERROR</b>: 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 "<b>ERROR</b>: 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, "<$file"); +open(FP, "<$file"); $bytes = sysread(FP, $buffer, $size); close(FP); return $buffer; @@ -352,7 +352,7 @@ while (1) { exit(0); ##EOF## -
We use the following ruleset to expand the tilde URLs into the above layout.
-=STRING
we can
do time-dependent redirects:
-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.
-Later in your ruleset you might check for this environment variable using a RewriteCond:
-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:
To redirect http
URLs to https
, do the
following:
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.
@@ -175,18 +172,18 @@ seems like the right approach.
href="flags.html#flag_p">[P] flag to pass rewritten URIs throughHowever, in many cases, when there is no actual pattern matching
needed, as in the example shown above, the
Note that whether you use
You may need to use RewriteRule
instead when there are
other RewriteRule
s 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
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. -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:
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:
-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.
-Note that this same effect can be obtained using
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.
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:
-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: -
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:
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
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.
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:
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
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:
-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
This technique is useful because 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:
It is probably easier to accomplish this kind of configuration using
the
For example, you might use the following technique to serve Perl source code as plain text, if requested in a particular way:
-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:
-Please note that this is a trivial example, and could be better done
using 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 :
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:
-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.
-We rewrite the old URL to the new one internally via the following rule:
-We force a HTTP redirect to the new URL which leads to a change of the browsers and thus the users view:
-/~quux/foo.cgi
.
-You can alternatively accomplish this using the
Or, for example, to redirect a portion of your site to HTTPS, you might do the following:
-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:
-And for a site running on port 80
-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:
-These rulesets will work either in your main server configuration
file, or in a We'll use a .htaccess
file placed in the
-
-# 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
-RewriteMap multiplex txt:/path/to/map.mirrors
-RewriteCond %{REMOTE_HOST} ([a-z]+)$ [NC]
+
@@ -484,16 +472,16 @@ com http://www.example.com/
All other browsers receive page foo.32.html
.
This is done with the following ruleset:
/puppies
and /canines
by the canonical /dogs
.
-/about/
:
-Note that this can also be handled using the
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
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:
-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:
-Note that these rulesets can be used in a .htaccess
file, as well as in a <Directory> block.
The syntax of the RewriteMap
directive is as
follows:
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
You would then be able to use this map in a
A default value can be specified in the event that nothing is found in the map:
-@@ -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
+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:
-
Configuration directives
+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:
-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:
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
+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
+dash2under.pl
+If a query returns more than one row, a random row from the result set is used.
-ScriptAlias
directives, we must have
mod_rewrite
explicitly enact those mappings.
-The httpd.conf
should contain the following: