We use the following ruleset to expand the tilde URLs
into the above layout.
-$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 96d13c88d3e..fa79a6fa589 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 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 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.xml b/docs/manual/rewrite/flags.xml
index 9d846f503fb..ae538c44b19 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,10 +251,10 @@ 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
@@ -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
+
+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/proxy.xml b/docs/manual/rewrite/proxy.xml
index 41b2d56fef6..6d8a314a768 100644
--- a/docs/manual/rewrite/proxy.xml
+++ b/docs/manual/rewrite/proxy.xml
@@ -65,24 +65,24 @@ A number of recipes are provided that describe common scenarios.
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 a5db6e8ff35..3d474323174 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,9 @@ RewriteRule ^/foo\.html$ bar.html [
-
+
Redirect /foo.html /bar.html
-
+
@@ -145,18 +145,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 +186,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 +217,21 @@ 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>
+
+# 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>
-
+
Discussion
@@ -279,64 +279,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 +337,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 +416,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 +476,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 +516,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 +524,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 +553,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 +590,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 c7f50880a54..e03df546791 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
@@ -142,10 +134,10 @@ 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
@@ -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
@@ -214,12 +204,12 @@ 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
@@ -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,10 @@ 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
@@ -366,12 +356,12 @@ 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
@@ -380,7 +370,6 @@ by many requests.
print $_;
}
-
Caution!
@@ -423,9 +412,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/vhosts.xml b/docs/manual/rewrite/vhosts.xml
index 1c4e05174f8..f337263d07d 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]
-
+