From: Rich Bowen A mapping is maintained, from users to target servers, in
external map files. They look like: We put this into a to thus every URL path need not be valid on every backend physical
host. The following ruleset does this for us with the help of the map
files assuming that server0 is a default server which will be used if
a user has no entry in the map: Here a request for We'll use If you want one particular server to get more of the load than the
others, add it more times to the list.
-user1 physical_host_of_user1
-user2 physical_host_of_user2
+
+user1 physical_host_of_user1
+user2 physical_host_of_user2
: :
-map.users-to-hosts file. The
aim is to map;
+
/u/user1/anypath
-
+
http://physical_host_of_user1/u/user/anypath
-
-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_FILENAME} !-s
+
+# This example is valid in per-directory context only
+RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^page\.html$ page.cgi [T=application/x-httpd-cgi,L]
-page.html leads to an
internal run of a corresponding page.cgi if
@@ -159,20 +159,20 @@ RewriteRule ^page\.html$ page.cgi [
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:
-## serverlist.txt
-
-servers one.example.com|two.example.com|three.example.com
-
+## serverlist.txt
+
+servers one.example.com|two.example.com|three.example.com
+
++
RewriteRule ^(/[uge]/[^/]+/?.*):refresh /internal/cgi/apache/nph-refresh?f=$1 -
Now when we reference the URL
-++
/u/foo/bar/page.html:refresh -
this leads to the internal invocation of the URL
-++
/internal/cgi/apache/nph-refresh?f=/u/foo/bar/page.html -
The only missing part is the NPH-CGI script. Although one would usually say "left as an exercise to the reader" @@ -364,10 +364,10 @@ exit(0);
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
@@ -468,17 +468,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 -
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] -
-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> -RewriteEngine on -RewriteBase /var/www/htdocs - -RewriteCond $1.php -f -RewriteCond $1.html !-f -RewriteRule ^(.*).html$ $1.php ++
+# 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
+ </Directory> -
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 @@ -341,11 +343,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 DocumentRoot of the server.
-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]
-
+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
+
-de http://www.example.de/
-uk http://www.example.uk/
-com http://www.example.com/
+
+## map.mirrors -- Multiplexing Map
+
+de http://www.example.de/
+uk http://www.example.uk/
+com http://www.example.com/
##EOF##
-
+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]
-
+/puppies and /canines
by the canonical /dogs.
-++
RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R] -
++
RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2 -
/about/:
--RewriteEngine on ++
+RewriteEngine on
RewriteRule ^/$ /about/ [R] -
Note that this can also be handled using the RedirectMatch directive:
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] -
Note that these rulesets can be uses in a .htaccess
file, as well as in a <Directory> block.
The file /etc/apache2/productmap.txt then contains
the following:
-## -## productmap.txt - Product to ID map file -## - -television 993 -stereo 198 -fishingrod 043 -basketball 418 ++Product to ID map
+##
+## productmap.txt - Product to ID map file
+##
+
+television 993
+stereo 198
+fishingrod 043
+basketball 418
telephone 328 -
Thus, when http://example.com/product/television is
requested, the RewriteRule is applied, and the request
@@ -207,14 +207,14 @@ telephone 328
to one of the servers in the 'static' pool, while everything
else is sent to one of the 'dynamic' pool.
-## -## map.txt -- rewriting map -## - -static www1|www2|www3|www4 ++Rewrite map file
+##
+## map.txt -- rewriting map
+##
+
+static www1|www2|www3|www4
dynamic www5|www6 -
RewriteMap servers rnd:/path/to/file/map.txt