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
@@ -115,14 +118,15 @@ 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} !^$
-RewriteCond %{HTTP_REFERER} !www.example.com [NC]
+
+RewriteCond %{HTTP_REFERER} !^$
+RewriteCond %{HTTP_REFERER} !www.example.com [NC]
RewriteRule \.(gif|jpg|png)$ - [F,NC]
-
-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]
-
-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]
-
- 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>
+
+
@@ -165,11 +169,12 @@ 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]
-
- 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
@@ -216,13 +222,14 @@ RewriteRule ^/secret/files/ - [F]
-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]
-
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.html.en b/docs/manual/rewrite/advanced.html.en
index a0edc75dc39..8d2415852e0 100644
--- a/docs/manual/rewrite/advanced.html.en
+++ b/docs/manual/rewrite/advanced.html.en
@@ -91,13 +91,14 @@ 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 We'll use
##
@@ -270,17 +277,18 @@ bsdti1.sdm.de -
-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]
-
-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
-
-# 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]
--U operator determines whether the test string
(in this case, REQUEST_URI) is a valid URL. It does
@@ -167,12 +169,13 @@ RewriteRule ^(.+)\.html$ /regenerate_page.cgi [PT,L]
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:
+
RewriteRule ^(/[uge]/[^/]+/?.*):refresh /internal/cgi/apache/nph-refresh?f=$1
-
Now when we reference the URL
@@ -243,7 +247,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
@@ -345,7 +349,8 @@ while (1) {
exit(0);
##EOF##
-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
-
=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
@@ -478,17 +485,19 @@ 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.html.en b/docs/manual/rewrite/avoid.html.en
index 083ec344531..5185a43752e 100644
--- a/docs/manual/rewrite/avoid.html.en
+++ b/docs/manual/rewrite/avoid.html.en
@@ -83,25 +83,24 @@ 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
@@ -126,7 +125,8 @@ is possible to perform this mapping with mod_rewrite,
and performance.
-Alias /cats /var/www/virtualhosts/felines/htdocs
+Alias /cats /var/www/virtualhosts/felines/htdocs
+
@@ -167,25 +167,28 @@ seems like the right approach.
RewriteRule provides the [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
@@ -207,11 +210,12 @@ variable or request header. This can be done more efficiently using the
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'">
+ RedirectMatch (.*) http://www.example.com$1
</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.html.en b/docs/manual/rewrite/flags.html.en index 2b195154c54..416a6de6a80 100644 --- a/docs/manual/rewrite/flags.html.en +++ b/docs/manual/rewrite/flags.html.en @@ -59,9 +59,8 @@ 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
@@ -91,9 +90,8 @@ 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 @@ -177,10 +175,11 @@ 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 @@ -265,10 +264,11 @@ 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.
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 @@ -313,9 +312,8 @@ 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.
@@ -327,9 +325,8 @@ 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
@@ -341,9 +338,10 @@ 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
@@ -390,11 +388,12 @@ redirects.
index.php, however, the 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]
-
-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
@@ -431,9 +429,8 @@ 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]+
-RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]
-
RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]+
The above example will redirect /anchor/xyz to
@@ -485,9 +481,8 @@ 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
@@ -534,10 +529,11 @@ use the [PT] flag to ensure that the
Alias is evaluated.
-Alias /icons /usr/local/apache/icons
+
+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 @@ -566,9 +562,8 @@ 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]
@@ -643,16 +638,17 @@ 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
RewriteRule immediately
@@ -662,26 +658,23 @@ 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>, <ElseIf>, and <Else> directives instead.
-# 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/intro.xml.fr b/docs/manual/rewrite/intro.xml.fr
index ac2e4a4ec60..8401ecc892f 100644
--- a/docs/manual/rewrite/intro.xml.fr
+++ b/docs/manual/rewrite/intro.xml.fr
@@ -1,7 +1,7 @@
-
+
diff --git a/docs/manual/rewrite/intro.xml.meta b/docs/manual/rewrite/intro.xml.meta
index 5aaac0fc867..ce245b2841c 100644
--- a/docs/manual/rewrite/intro.xml.meta
+++ b/docs/manual/rewrite/intro.xml.meta
@@ -8,6 +8,6 @@
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/
-
We rewrite the old URL to the new one internally via the following rule:
-
-RewriteEngine on
+
+RewriteEngine on
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]
-
+
Redirect /foo.html /bar.html
-
-RewriteEngine on
+With mod_rewrite
+RewriteEngine on
RewriteRule ^/docs/(.+) http://new.example.com/docs/$1 [R,L]
-
+With RedirectMatch
RedirectMatch ^/docs/(.*) http://new.example.com/docs/$1
-
+With Redirect
Redirect /docs/ http://new.example.com/docs/
-
/~quux/foo.cgi.
-
-RewriteEngine on
-RewriteBase /~quux/
+
+RewriteEngine on
+RewriteBase /~quux/
RewriteRule ^foo\.html$ foo.cgi [H=cgi-script]
-
-# backward compatibility ruleset for
-# rewriting document.html to document.php
-# when and only when document.php exists
-<Directory /var/www/htdocs>
+
+# 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
+RewriteEngine on
+RewriteBase /var/www/htdocs
+
+RewriteCond $1.php -f
+RewriteCond $1.html !-f
+RewriteRule ^(.*).html$ $1.php
</Directory>
-
-<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
@@ -346,11 +351,12 @@ 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 DocumentRoot of the server.
@@ -380,23 +386,24 @@ RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
resource, and, if not finding it in either place, will attempt to
just serve it out of the location requested.
-
-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 +431,14 @@ 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 +492,17 @@ 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 +533,8 @@ 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 +542,8 @@ 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,16 +572,16 @@ 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
@@ -602,36 +609,33 @@ 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.html.en b/docs/manual/rewrite/rewritemap.html.en
index e540e234214..3e4f62429b9 100644
--- a/docs/manual/rewrite/rewritemap.html.en
+++ b/docs/manual/rewrite/rewritemap.html.en
@@ -64,9 +64,8 @@ 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
@@ -90,21 +89,18 @@ 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
@@ -146,10 +142,11 @@ may be used, and give examples of each.
product IDs for easier-to-remember URLs, using the following
recipe:
- Product to ID configuration
- RewriteMap product2id txt:/etc/apache2/productmap.txt
+ #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
@@ -179,9 +176,8 @@ 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
@@ -219,12 +215,13 @@ static www1|www2|www3|www4
dynamic www5|www6
- 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]
-
+ #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]
+
+
So, when an image is requested and the first of these rules is
matched, RewriteMap looks up the string
@@ -255,9 +252,10 @@ 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 httxt2dbm utility that is
@@ -275,9 +273,10 @@ $ httxt2dbm -i mapfile.txt -o mapfile.map
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
@@ -326,10 +325,11 @@ 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
+ 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
@@ -369,12 +369,13 @@ 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}}
-
+ #Rewrite configuration
+RewriteMap d2u prg:/www/bin/dash2under.pl
+RewriteRule - ${d2u:%{REQUEST_URI}}
+
+
- dash2under.pl
+ dash2under.pl
#!/usr/bin/perl
$| = 1; # Turn off I/O buffering
@@ -384,7 +385,6 @@ by many requests.
}
-
Caution!
@@ -426,9 +426,10 @@ 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/vhosts.html.en b/docs/manual/rewrite/vhosts.html.en
index 915df204c34..4025a848424 100644
--- a/docs/manual/rewrite/vhosts.html.en
+++ b/docs/manual/rewrite/vhosts.html.en
@@ -65,14 +65,15 @@ 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
@@ -122,39 +123,38 @@ 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]
+
+
@@ -177,27 +177,28 @@ 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]
-
+
+