From: Ralf S. Engelschall Date: Thu, 10 Feb 2000 16:35:48 +0000 (+0000) Subject: Cleanup my old physical HTML markup into a logical one to X-Git-Tag: 1.3.12~32 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b5f690162c5e3d92834ea3def79ac44289b00947;p=apache Cleanup my old physical HTML markup into a logical one to fit better into the Apache documentation. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@84601 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/docs/manual/misc/rewriteguide.html b/docs/manual/misc/rewriteguide.html index 7804158498..e32a6dab38 100644 --- a/docs/manual/misc/rewriteguide.html +++ b/docs/manual/misc/rewriteguide.html @@ -28,14 +28,14 @@ December 1997

-This document supplements the mod_rewrite reference documentation. It describes +This document supplements the mod_rewrite reference documentation. It describes how one can use Apache's mod_rewrite to solve typical URL-based problems webmasters are usually confronted with in practice. I give detailed descriptions on how to solve each problem by configuring URL rewriting rulesets. -

Introduction to mod_rewrite

+

Introduction to mod_rewrite

The Apache module mod_rewrite is a killer one, i.e. it is a really sophisticated module which provides a powerful way to do URL manipulations. @@ -50,7 +50,7 @@ first time and never use it again or love it for the rest of your life because of its power. This paper tries to give you a few initial success events to avoid the first case by presenting already invented solutions to you. -

Practical Solutions

+

Practical Solutions

Here come a lot of practical solutions I've either invented myself or collected from other peoples solutions in the past. Feel free to learn the @@ -60,7 +60,7 @@ black magic of URL rewriting from these examples. ATTENTION: Depending on your server-configuration it can be necessary to slightly change the examples for your situation, e.g. adding the [PT] flag when additionally using mod_alias and mod_userdir, etc. Or rewriting a ruleset -to fit in .htaccess context instead of per-server context. Always try +to fit in .htaccess context instead of per-server context. Always try to understand what a particular ruleset really does before you use it. It avoid problems. @@ -83,12 +83,12 @@ supplied with the request he should finally see the canonical one only.
We do an external HTTP redirect for all non-canonical URLs to fix them in the location view of the Browser and for all subsequent requests. In the example -ruleset below we replace /~user by the canonical /u/user and -fix a missing trailing slash for /u/user. +ruleset below we replace /~user by the canonical /u/user and +fix a missing trailing slash for /u/user.

-RewriteRule   ^/~([^/]+)/?(.*)    /u/$1/$2  [R]
-RewriteRule   ^/([uge])/([^/]+)$  /$1/$2/   [R]
+RewriteRule   ^/~([^/]+)/?(.*)    /u/$1/$2  [R]
+RewriteRule   ^/([uge])/([^/]+)$  /$1/$2/   [R]
 
@@ -126,26 +126,26 @@ RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]

Description:
Usually the DocumentRoot of the webserver directly relates to the URL -``/''. But often this data is not really of top-level priority, it is +``/''. But often this data is not really of top-level priority, it is perhaps just one entity of a lot of data pools. For instance at our Intranet -sites there are /e/www/ (the homepage for WWW), /e/sww/ (the +sites there are /e/www/ (the homepage for WWW), /e/sww/ (the homepage for the Intranet) etc. Now because the data of the DocumentRoot stays -at /e/www/ we had to make sure that all inlined images and other +at /e/www/ we had to make sure that all inlined images and other stuff inside this data pool work for subsequent requests.

Solution:
-We just redirect the URL / to /e/www/. While is seems +We just redirect the URL / to /e/www/. While is seems trivial it is actually trivial with mod_rewrite, only. Because the typical -old mechanisms of URL Aliases (as provides by mod_alias and friends) -only used prefix matching. With this you cannot do such a redirection +old mechanisms of URL Aliases (as provides by mod_alias and friends) +only used prefix matching. With this you cannot do such a redirection because the DocumentRoot is a prefix of all URLs. With mod_rewrite it is really trivial:

 RewriteEngine on
-RewriteRule   ^/$  /e/www/  [R]
+RewriteRule   ^/$  /e/www/  [R]
 
@@ -159,9 +159,9 @@ RewriteRule ^/$ /e/www/ [R]

Every webmaster can sing a song about the problem of the trailing slash on URLs referencing directories. If they are missing, the server dumps an error, -because if you say /~quux/foo instead of -/~quux/foo/ then the server searches for a file named -foo. And because this file is a directory it complains. Actually +because if you say /~quux/foo instead of +/~quux/foo/ then the server searches for a file named +foo. And because this file is a directory it complains. Actually is tries to fix it themself in most of the cases, but sometimes this mechanism need to be emulated by you. For instance after you have done a lot of complicated URL rewritings to CGI scripts etc. @@ -175,27 +175,27 @@ so the browser correctly requests subsequent images etc. If we only did a internal rewrite, this would only work for the directory page, but would go wrong when any images are included into this page with relative URLs, because the browser would request an in-lined object. For instance, a request for -image.gif in /~quux/foo/index.html would become -/~quux/image.gif without the external redirect! +image.gif in /~quux/foo/index.html would become +/~quux/image.gif without the external redirect!

So, to do this trick we write:

 RewriteEngine  on
 RewriteBase    /~quux/
-RewriteRule    ^foo$  foo/  [R]
+RewriteRule    ^foo$  foo/  [R]
 

The crazy and lazy can even do the following in the top-level -.htaccess file of their homedir. But notice that this creates some +.htaccess file of their homedir. But notice that this creates some processing overhead.

 RewriteEngine  on
 RewriteBase    /~quux/
-RewriteCond    %{REQUEST_FILENAME}  -d
-RewriteRule    ^(.+[^/])$           $1/  [R]
+RewriteCond    %{REQUEST_FILENAME}  -d
+RewriteRule    ^(.+[^/])$           $1/  [R]
 
@@ -209,7 +209,7 @@ RewriteRule ^(.+[^/])$ $1/ [R]

We want to create a homogenous and consistent URL layout over all WWW servers on a Intranet webcluster, i.e. all URLs (per definition server local and thus -server dependent!) become actually server independed! What we want is +server dependent!) become actually server independed! What we want is to give the WWW namespace a consistent server-independend layout: no URL should have to include any physically correct target server. The cluster itself should drive us automatically to the physical target host. @@ -227,7 +227,7 @@ user2 server_of_user2 : :

-We put them into files map.xxx-to-host. Second we need to instruct +We put them into files map.xxx-to-host. Second we need to instruct all servers to redirect URLs of the forms

@@ -255,9 +255,9 @@ RewriteMap      user-to-host   txt:/path/to/map.user-to-host
 RewriteMap     group-to-host   txt:/path/to/map.group-to-host
 RewriteMap    entity-to-host   txt:/path/to/map.entity-to-host
 
-RewriteRule   ^/u/([^/]+)/?(.*)   http://${user-to-host:$1|server0}/u/$1/$2
-RewriteRule   ^/g/([^/]+)/?(.*)  http://${group-to-host:$1|server0}/g/$1/$2
-RewriteRule   ^/e/([^/]+)/?(.*) http://${entity-to-host:$1|server0}/e/$1/$2
+RewriteRule   ^/u/([^/]+)/?(.*)   http://${user-to-host:$1|server0}/u/$1/$2
+RewriteRule   ^/g/([^/]+)/?(.*)  http://${group-to-host:$1|server0}/g/$1/$2
+RewriteRule   ^/e/([^/]+)/?(.*) http://${entity-to-host:$1|server0}/e/$1/$2
 
 RewriteRule   ^/([uge])/([^/]+)/?$          /$1/$2/.www/
 RewriteRule   ^/([uge])/([^/]+)/([^.]+.+)   /$1/$2/.www/$3\
@@ -281,12 +281,12 @@ replace the old one over time.
 
Solution:
The solution is trivial with mod_rewrite. On the old webserver we just -redirect all /~user/anypath URLs to -http://newserver/~user/anypath. +redirect all /~user/anypath URLs to +http://newserver/~user/anypath.

 RewriteEngine on
-RewriteRule   ^/~(.+)  http://newserver/~$1  [R,L]
+RewriteRule   ^/~(.+)  http://newserver/~$1  [R,L]
 
@@ -300,9 +300,9 @@ RewriteRule ^/~(.+) http://newserver/~$1 [R,L]

Some sites with thousend of users usually use a structured homedir layout, i.e. each homedir is in a subdirectory which begins for instance with the -first character of the username. So, /~foo/anypath is -/home/f/foo/.www/anypath while /~bar/anypath is -/home/b/bar/.www/anypath. +first character of the username. So, /~foo/anypath is +/home/f/foo/.www/anypath while /~bar/anypath is +/home/b/bar/.www/anypath.

Solution: @@ -312,7 +312,7 @@ layout.

 RewriteEngine on
-RewriteRule   ^/~(([a-z])[a-z0-9]+)(.*)  /home/$2/$1/.www$3
+RewriteRule   ^/~(([a-z])[a-z0-9]+)(.*)  /home/$2/$1/.www$3
 
@@ -325,10 +325,10 @@ RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /home/$2/$1/.www$3

Description:
This really is a hardcore example: a killer application which heavily uses -per-directory RewriteRules to get a smooth look and feel on the Web +per-directory RewriteRules to get a smooth look and feel on the Web while its data structure is never touched or adjusted. -Background: net.sw is my archive of freely available Unix +Background: net.sw is my archive of freely available Unix software packages, which I started to collect in 1992. It is both my hobby and job to to this, because while I'm studying computer science I have also worked for many years as a system and network administrator in my spare time. Every @@ -355,8 +355,8 @@ drwxrwxr-x 10 netsw users 512 Jul 9 14:08 X11/

In July 1996 I decided to make this 350 MB archive public to the world via a -nice Web interface ( -http://net.sw.engelschall.com/net.sw/). "Nice" means that I wanted to +nice Web interface ( +http://net.sw.engelschall.com/net.sw/). "Nice" means that I wanted to offer a interface where you can browse directly through the archive hierarchy. And "nice" means that I didn't wanted to change anything inside this hierarchy - not even by putting some CGI scripts at the top of it. Why? Because the @@ -368,7 +368,7 @@ want any Web or CGI stuuf to be there.

The solution has two parts: The first is a set of CGI scripts which create all the pages at all directory levels on-the-fly. I put them under -/e/netsw/.www/ as follows: +/e/netsw/.www/ as follows:

 -rw-r--r--   1 netsw  users    1318 Aug  1 18:10 .wwwacl
@@ -386,18 +386,18 @@ drwxr-xr-x   2 netsw  users     512 Jul  8 23:47 netsw-img/
 -rw-r--r--   1 netsw  users     234 Jul 30 16:35 netsw-unlimit.lst
 

-The DATA/ subdirectory holds the above directory structure, i.e. the -real net.sw stuff and gets automatically updated via -rdist from time to time. +The DATA/ subdirectory holds the above directory structure, i.e. the +real net.sw stuff and gets automatically updated via +rdist from time to time. The second part of the problem remains: how to link these two structures -together into one smooth-looking URL tree? We want to hide the DATA/ +together into one smooth-looking URL tree? We want to hide the DATA/ directory from the user while running the appropriate CGI scripts for the various URLs. Here is the solution: first I put the following into the per-directory configuration file in the Document Root of the server to rewrite the announced -URL /net.sw/ to the internal path /e/netsw: +URL /net.sw/ to the internal path /e/netsw:

 RewriteRule  ^net.sw$       net.sw/        [R]
@@ -407,7 +407,7 @@ RewriteRule  ^net.sw/(.*)$  e/netsw/$1
 

The first rule is for requests which miss the trailing slash! The second rule does the real thing. And then comes the killer configuration which stays in -the per-directory config file /e/netsw/.www/.wwwacl: +the per-directory config file /e/netsw/.www/.wwwacl:

 Options       ExecCGI FollowSymLinks Includes MultiViews 
@@ -461,11 +461,11 @@ Some hints for interpretation:
 
When switching from the NCSA webserver to the more modern Apache webserver a lot of people want a smooth transition. So they want pages which use their old -NCSA imagemap program to work under Apache with the modern -mod_imap. The problem is that there are a lot of -hyperlinks around which reference the imagemap program via -/cgi-bin/imagemap/path/to/page.map. Under Apache this -has to read just /path/to/page.map. +NCSA imagemap program to work under Apache with the modern +mod_imap. The problem is that there are a lot of +hyperlinks around which reference the imagemap program via +/cgi-bin/imagemap/path/to/page.map. Under Apache this +has to read just /path/to/page.map.

Solution: @@ -499,13 +499,13 @@ RewriteEngine on # first try to find it in custom/... # ...and if found stop and be happy: -RewriteCond /your/docroot/dir1/%{REQUEST_FILENAME} -f -RewriteRule ^(.+) /your/docroot/dir1/$1 [L] +RewriteCond /your/docroot/dir1/%{REQUEST_FILENAME} -f +RewriteRule ^(.+) /your/docroot/dir1/$1 [L] # second try to find it in pub/... # ...and if found stop and be happy: -RewriteCond /your/docroot/dir2/%{REQUEST_FILENAME} -f -RewriteRule ^(.+) /your/docroot/dir2/$1 [L] +RewriteCond /your/docroot/dir2/%{REQUEST_FILENAME} -f +RewriteRule ^(.+) /your/docroot/dir2/$1 [L] # else go on for other Alias or ScriptAlias directives, # etc. @@ -530,13 +530,13 @@ strip out this information.
We use a rewrite rule to strip out the status information and remember it via an environment variable which can be later dereferenced from within XSSI or -CGI. This way a URL /foo/S=java/bar/ gets translated to -/foo/bar/ and the environment variable named STATUS is set +CGI. This way a URL /foo/S=java/bar/ gets translated to +/foo/bar/ and the environment variable named STATUS is set to the value "java".

 RewriteEngine on
-RewriteRule   ^(.*)/S=([^/]+)/(.*)    $1/$3 [E=STATUS:$2]
+RewriteRule   ^(.*)/S=([^/]+)/(.*)    $1/$3 [E=STATUS:$2]
 
@@ -548,7 +548,7 @@ RewriteRule ^(.*)/S=([^/]+)/(.*) $1/$3 [E=STATUS:$2]

Description:
-Assume that you want to provide www.username.host.domain.com +Assume that you want to provide www.username.host.domain.com for the homepage of username via just DNS A records to the same machine and without any virtualhosts on this machine. @@ -557,14 +557,14 @@ without any virtualhosts on this machine.
For HTTP/1.0 requests there is no solution, but for HTTP/1.1 requests which contain a Host: HTTP header we can use the following ruleset to rewrite -http://www.username.host.com/anypath internally to -/home/username/anypath: +http://www.username.host.com/anypath internally to +/home/username/anypath:

 RewriteEngine on
-RewriteCond   %{HTTP_HOST}                 ^www\.[^.]+\.host\.com$
+RewriteCond   %{HTTP_HOST}                 ^www\.[^.]+\.host\.com$
 RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
-RewriteRule   ^www\.([^.]+)\.host\.com(.*) /home/$1$2
+RewriteRule   ^www\.([^.]+)\.host\.com(.*) /home/$1$2
 

@@ -577,8 +577,8 @@ RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2
Description:
We want to redirect homedir URLs to another webserver -www.somewhere.com when the requesting user does not stay in the local -domain ourdomain.com. This is sometimes used in virtual host +www.somewhere.com when the requesting user does not stay in the local +domain ourdomain.com. This is sometimes used in virtual host contexts.

@@ -588,7 +588,7 @@ Just a rewrite condition:

 RewriteEngine on
-RewriteCond   %{REMOTE_HOST}  !^.+\.ourdomain\.com$
+RewriteCond   %{REMOTE_HOST}  !^.+\.ourdomain\.com$
 RewriteRule   ^(/~.+)         http://www.somewhere.com/$1 [R,L]
 
@@ -614,8 +614,8 @@ error safe:

 RewriteEngine on
-RewriteCond   /your/docroot/%{REQUEST_FILENAME} !-f
-RewriteRule   ^(.+)                             http://webserverB.dom/$1
+RewriteCond   /your/docroot/%{REQUEST_FILENAME} !-f
+RewriteRule   ^(.+)                             http://webserverB.dom/$1
 

@@ -625,8 +625,8 @@ homedirs, etc.) there is better variant:

 RewriteEngine on
-RewriteCond   %{REQUEST_URI} !-U
-RewriteRule   ^(.+)          http://webserverB.dom/$1
+RewriteCond   %{REQUEST_URI} !-U
+RewriteRule   ^(.+)          http://webserverB.dom/$1
 

@@ -657,7 +657,7 @@ also escape the hash character. How can we redirect to such a URL?

We have to use a kludge by the use of a NPH-CGI script which does the redirect itself. Because here no escaping is done (NPH=non-parseable headers). First -we introduce a new URL scheme xredirect: by the following per-server +we introduce a new URL scheme xredirect: by the following per-server config-line (should be one of the last rewrite rules):

@@ -666,8 +666,8 @@ RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 \
 

-This forces all URLs prefixed with xredirect: to be piped through the -nph-xredirect.cgi program. And this program just looks like: +This forces all URLs prefixed with xredirect: to be piped through the +nph-xredirect.cgi program. And this program just looks like:

 
@@ -691,7 +691,7 @@ print "<title>302 Moved Temporarily (EXTENDED)</title>\n";
 print "</head>\n";
 print "<body>\n";
 print "<h1>Moved Temporarily (EXTENDED)</h1>\n";
-print "The document has moved <a href=\"$url\">here</a>.<p>\n";
+print "The document has moved <a HREF=\"$url\">here</a>.<p>\n";
 print "</body>\n";
 print "</html>\n";
 
@@ -702,7 +702,7 @@ print "</html>\n";
 

This provides you with the functionality to do redirects to all URL schemes, i.e. including the one which are not directly accepted by mod_rewrite. For -instance you can now also redirect to news:newsgroup via +instance you can now also redirect to news:newsgroup via

 RewriteRule ^anyurl  xredirect:news:newsgroup
@@ -710,7 +710,7 @@ RewriteRule ^anyurl  xredirect:news:newsgroup
 
 

Notice: You have not to put [R] or [R,L] to the above rule because the -xredirect: need to be expanded later by our special "pipe through" +xredirect: need to be expanded later by our special "pipe through" rule above. @@ -722,8 +722,8 @@ rule above.

Description:
-Do you know the great CPAN (Comprehensive Perl Archive Network) under http://www.perl.com/CPAN? This does a +Do you know the great CPAN (Comprehensive Perl Archive Network) under http://www.perl.com/CPAN? This does a redirect to one of several FTP servers around the world which carry a CPAN mirror and is approximately near the location of the requesting client. Actually this can be called an FTP access multiplexing service. While CPAN @@ -741,7 +741,7 @@ ruleset we can use this top-level domain as a key to our multiplexing map. RewriteEngine on RewriteMap multiplex txt:/path/to/map.cxan RewriteRule ^/CxAN/(.*) %{REMOTE_HOST}::$1 [C] -RewriteRule ^.+\.([a-zA-Z]+)::(.*)$ ${multiplex:$1|ftp.default.dom}$2 [R,L] +RewriteRule ^.+\.([a-zA-Z]+)::(.*)$ ${multiplex:$1|ftp.default.dom}$2 [R,L]

@@ -772,7 +772,7 @@ How can it be done via mod_rewrite?
 

Solution:
-There are a lot of variables named TIME_xxx for rewrite conditions. +There are a lot of variables named TIME_xxx for rewrite conditions. In conjunction with the special lexicographic comparison patterns <STRING, >STRING and =STRING we can do time-dependend redirects: @@ -785,9 +785,9 @@ RewriteRule ^foo\.html$ foo.night.html

-This provides the content of foo.day.html under the URL -foo.html from 07:00-19:00 and at the remaining time the contents of -foo.night.html. Just a nice feature for a homepage... +This provides the content of foo.day.html under the URL +foo.html from 07:00-19:00 and at the remaining time the contents of +foo.night.html. Just a nice feature for a homepage... @@ -837,8 +837,8 @@ RewriteRule ^(.*)$ $1.html

Description:
-Assume we have recently renamed the page bar.html to -foo.html and now want to provide the old URL for backward +Assume we have recently renamed the page bar.html to +foo.html and now want to provide the old URL for backward compatibility. Actually we want that users of the old URL even not recognize that the pages was renamed. @@ -850,7 +850,7 @@ We rewrite the old URL to the new one internally via the following rule:

 RewriteEngine  on
 RewriteBase    /~quux/
-RewriteRule    ^foo\.html$  bar.html
+RewriteRule    ^foo\.html$  bar.html
 

@@ -862,8 +862,8 @@ RewriteRule ^foo\.html$ bar.html
Description:
-Assume again that we have recently renamed the page bar.html to -foo.html and now want to provide the old URL for backward +Assume again that we have recently renamed the page bar.html to +foo.html and now want to provide the old URL for backward compatibility. But this time we want that the users of the old URL get hinted to the new one, i.e. their browsers Location field should change, too. @@ -876,7 +876,7 @@ browsers and thus the users view:

 RewriteEngine  on
 RewriteBase    /~quux/
-RewriteRule    ^foo\.html$  bar.html  [R]
+RewriteRule    ^foo\.html$  bar.html  [R]
 

@@ -899,21 +899,21 @@ browsers and a average feature version for all others. We cannot use content negotiation because the browsers do not provide their type in that form. Instead we have to act on the HTTP header "User-Agent". The following condig does the following: If the HTTP header "User-Agent" -begins with "Mozilla/3", the page foo.html is rewritten to -foo.NS.html and and the rewriting stops. If the browser is "Lynx" or -"Mozilla" of version 1 or 2 the URL becomes foo.20.html. All other -browsers receive page foo.32.html. This is done by the following +begins with "Mozilla/3", the page foo.html is rewritten to +foo.NS.html and and the rewriting stops. If the browser is "Lynx" or +"Mozilla" of version 1 or 2 the URL becomes foo.20.html. All other +browsers receive page foo.32.html. This is done by the following ruleset:

-RewriteCond %{HTTP_USER_AGENT}  ^Mozilla/3.*
-RewriteRule ^foo\.html$         foo.NS.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]
+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]
+RewriteRule ^foo\.html$         foo.32.html          [L]
 
@@ -926,9 +926,9 @@ RewriteRule ^foo\.html$ foo.32.html [L]

Description:
Assume there are nice webpages on remote hosts we want to bring into our -namespace. For FTP servers we would use the mirror program which +namespace. For FTP servers we would use the mirror program which actually maintains an explicit up-to-date copy of the remote data on the local -machine. For a webserver we could use the program webcopy which acts +machine. For a webserver we could use the program webcopy which acts similar via HTTP. But both techniques have one major drawback: The local copy is always just as up-to-date as often we run the program. It would be much better if the mirror is not a static one we have to establish explicitly. @@ -945,13 +945,13 @@ webarea to our namespace by the use of the Proxy Throughput feature

 RewriteEngine  on
 RewriteBase    /~quux/
-RewriteRule    ^hotsheet/(.*)$  http://www.tstimpreso.com/hotsheet/$1  [P]
+RewriteRule    ^hotsheet/(.*)$  http://www.tstimpreso.com/hotsheet/$1  [P]
 

 RewriteEngine  on
 RewriteBase    /~quux/
-RewriteRule    ^usa-news\.html$   http://www.quux-corp.com/news/index.html  [P]
+RewriteRule    ^usa-news\.html$   http://www.quux-corp.com/news/index.html  [P]
 
@@ -985,9 +985,9 @@ RewriteRule ^http://www\.remotesite\.com/(.*)$ /mirror/of/remotesite/$1

Description:
This is a tricky way of virtually running a corporates (external) Internet -webserver (www.quux-corp.dom), while actually keeping and maintaining +webserver (www.quux-corp.dom), while actually keeping and maintaining its data on a (internal) Intranet webserver -(www2.quux-corp.dom) which is protected by a firewall. The +(www2.quux-corp.dom) which is protected by a firewall. The trick is that on the external webserver we retrieve the requested data on-the-fly from the internal one. @@ -1000,8 +1000,8 @@ from it. For a packet-filtering firewall we could for instance configure a firewall ruleset like the following:

-ALLOW Host www.quux-corp.dom Port >1024 --> Host www2.quux-corp.dom Port 80  
-DENY  Host *                 Port *     --> Host www2.quux-corp.dom Port 80
+ALLOW Host www.quux-corp.dom Port >1024 --> Host www2.quux-corp.dom Port 80  
+DENY  Host *                 Port *     --> Host www2.quux-corp.dom Port 80
 

@@ -1011,9 +1011,9 @@ proxy throughput feature:

 RewriteRule ^/~([^/]+)/?(.*)          /home/$1/.www/$2
-RewriteCond %{REQUEST_FILENAME}       !-f
-RewriteCond %{REQUEST_FILENAME}       !-d
-RewriteRule ^/home/([^/]+)/.www/?(.*) http://www2.quux-corp.dom/~$1/pub/$2 [P]
+RewriteCond %{REQUEST_FILENAME}       !-f
+RewriteCond %{REQUEST_FILENAME}       !-d
+RewriteRule ^/home/([^/]+)/.www/?(.*) http://www2.quux-corp.dom/~$1/pub/$2 [P]
 
@@ -1025,8 +1025,8 @@ RewriteRule ^/home/([^/]+)/.www/?(.*) http://www2.quux-corp.dom/~$1/pub/$

Description:
-Suppose we want to load balance the traffic to www.foo.com over -www[0-5].foo.com (a total of 6 servers). How can this be done? +Suppose we want to load balance the traffic to www.foo.com over +www[0-5].foo.com (a total of 6 servers). How can this be done?

Solution: @@ -1035,11 +1035,11 @@ There are a lot of possible solutions for this problem. We will discuss first a commonly known DNS-based variant and then the special one with mod_rewrite:
    -
  1. DNS Round-Robin +
  2. DNS Round-Robin

    The simplest method for load-balancing is to use the DNS round-robin feature -of BIND. Here you just configure www[0-9].foo.com as usual in your +of BIND. Here you just configure www[0-9].foo.com as usual in your DNS with A(address) records, e.g.

    @@ -1066,33 +1066,33 @@ www    IN  CNAME   www0.foo.com.
     
     

    Notice that this seems wrong, but is actually an intended feature of BIND and -can be used in this way. However, now when www.foo.com gets resolved, -BIND gives out www0-www6 - but in a slightly permutated/rotated order +can be used in this way. However, now when www.foo.com gets resolved, +BIND gives out www0-www6 - but in a slightly permutated/rotated order every time. This way the clients are spread over the various servers. But notice that this not a perfect load balancing scheme, because DNS resolve information gets cached by the other nameservers on the net, so once a client -has resolved www.foo.com to a particular wwwN.foo.com, all -subsequent requests also go to this particular name wwwN.foo.com. But +has resolved www.foo.com to a particular wwwN.foo.com, all +subsequent requests also go to this particular name wwwN.foo.com. But the final result is ok, because the total sum of the requests are really spread over the various webservers.

    -

  3. DNS Load-Balancing +
  4. DNS Load-Balancing

    A sophisticated DNS-based method for load-balancing is to use the program -lbnamed which can be found at http://www.stanford.edu/~schemers/docs/lbnamed/lbnamed.html. +lbnamed which can be found at http://www.stanford.edu/~schemers/docs/lbnamed/lbnamed.html. It is a Perl 5 program in conjunction with auxilliary tools which provides a real load-balancing for DNS.

    -

  5. Proxy Throughput Round-Robin +
  6. Proxy Throughput Round-Robin

    In this variant we use mod_rewrite and its proxy throughput feature. First we -dedicate www0.foo.com to be actually www.foo.com by using a +dedicate www0.foo.com to be actually www.foo.com by using a single

    @@ -1100,11 +1100,11 @@ www    IN  CNAME   www0.foo.com.
     

    -entry in the DNS. Then we convert www0.foo.com to a proxy-only +entry in the DNS. Then we convert www0.foo.com to a proxy-only server, i.e. we configure this machine so all arriving URLs are just pushed -through the internal proxy to one of the 5 other servers (www1-www5). +through the internal proxy to one of the 5 other servers (www1-www5). To accomplish this we first establish a ruleset which contacts a load -balancing script lb.pl for all URLs. +balancing script lb.pl for all URLs.

     RewriteEngine on
    @@ -1113,7 +1113,7 @@ RewriteRule   ^/(.+)$ ${lb:$1}           [P,L]
     

    -Then we write lb.pl: +Then we write lb.pl:

     #!/path/to/perl
    @@ -1139,13 +1139,13 @@ while (<STDIN>) {
     

    -A last notice: Why is this useful? Seems like www0.foo.com still is +A last notice: Why is this useful? Seems like www0.foo.com still is overloaded? The answer is yes, it is overloaded, but with plain proxy throughput requests, only! All SSI, CGI, ePerl, etc. processing is completely done on the other machines. This is the essential point.

    -

  7. Hardware/TCP Round-Robin +
  8. Hardware/TCP Round-Robin

    There is a hardware solution available, too. Cisco has a beast called @@ -1285,34 +1285,34 @@ boring, so a lot of webmaster don't use them. Even Apache's Action handler feature for MIME-types is only appropriate when the CGI programs don't need special URLs (actually PATH_INFO and QUERY_STRINGS) as their input. -First, let us configure a new file type with extension .scgi -(for secure CGI) which will be processed by the popular cgiwrap +First, let us configure a new file type with extension .scgi +(for secure CGI) which will be processed by the popular cgiwrap program. The problem here is that for instance we use a Homogeneous URL Layout (see above) a file inside the user homedirs has the URL -/u/user/foo/bar.scgi. But cgiwrap needs the URL in the form -/~user/foo/bar.scgi/. The following rule solves the problem: +/u/user/foo/bar.scgi. But cgiwrap needs the URL in the form +/~user/foo/bar.scgi/. The following rule solves the problem:

    -RewriteRule ^/[uge]/([^/]+)/\.www/(.+)\.scgi(.*) ...
    -... /internal/cgi/user/cgiwrap/~$1/$2.scgi$3  [NS,T=application/x-http-cgi]
    +RewriteRule ^/[uge]/([^/]+)/\.www/(.+)\.scgi(.*) ...
    +... /internal/cgi/user/cgiwrap/~$1/$2.scgi$3  [NS,T=application/x-http-cgi]
     

    Or assume we have some more nifty programs: -wwwlog (which displays the access.log for a URL subtree and -wwwidx (which runs Glimpse on a URL subtree). We have to +wwwlog (which displays the access.log for a URL subtree and +wwwidx (which runs Glimpse on a URL subtree). We have to provide the URL area to these programs so they know on which area they have to act on. But usually this ugly, because they are all the times still requested from that areas, i.e. typically we would run -the swwidx program from within /u/user/foo/ via +the swwidx program from within /u/user/foo/ via hyperlink to

     /internal/cgi/user/swwidx?i=/u/user/foo/
     

    -which is ugly. Because we have to hard-code both the location of the -area and the location of the CGI inside the hyperlink. When we have to +which is ugly. Because we have to hard-code both the location of the +area and the location of the CGI inside the hyperlink. When we have to reorganise or area, we spend a lot of time changing the various hyperlinks.

    @@ -1327,10 +1327,10 @@ RewriteRule ^/([uge])/([^/]+)(/?.*):log /internal/cgi/user/wwwlog?f=/$1/$2$3

  9. -Now the hyperlink to search at /u/user/foo/ reads only +Now the hyperlink to search at /u/user/foo/ reads only

    -href="*"
    +HREF="*"
     

    which internally gets automatically transformed to @@ -1340,7 +1340,7 @@ which internally gets automatically transformed to

The same approach leads to an invocation for the access log CGI -program when the hyperlink :log gets used. +program when the hyperlink :log gets used. @@ -1351,21 +1351,21 @@ program when the hyperlink :log gets used.

Description:
-How can we transform a static page foo.html into a dynamic variant -foo.cgi in a seemless way, i.e. without notice by the browser/user. +How can we transform a static page foo.html into a dynamic variant +foo.cgi in a seemless way, i.e. without notice by the browser/user.

Solution:
We just rewrite the URL to the CGI-script and force the correct MIME-type so it gets really run as a CGI-script. This way a request to -/~quux/foo.html internally leads to the invokation of -/~quux/foo.cgi. +/~quux/foo.html internally leads to the invokation of +/~quux/foo.cgi.

 RewriteEngine  on
 RewriteBase    /~quux/
-RewriteRule    ^foo\.html$  foo.cgi  [T=application/x-httpd-cgi]
+RewriteRule    ^foo\.html$  foo.cgi  [T=application/x-httpd-cgi]
 

@@ -1390,18 +1390,18 @@ contents. Then the contents gets refreshed. This is done via the following ruleset:

-RewriteCond %{REQUEST_FILENAME}   !-s
-RewriteRule ^page\.html$          page.cgi   [T=application/x-httpd-cgi,L]
+RewriteCond %{REQUEST_FILENAME}   !-s
+RewriteRule ^page\.html$          page.cgi   [T=application/x-httpd-cgi,L]
 

-Here a request to page.html leads to a internal run of a -corresponding page.cgi if page.html is still missing or has -filesize null. The trick here is that page.cgi is a usual CGI script +Here a request to page.html leads to a internal run of a +corresponding page.cgi if page.html is still missing or has +filesize null. The trick here is that page.cgi is a usual CGI script which (additionally to its STDOUT) writes its output to the file -page.html. Once it was run, the server sends out the data of -page.html. When the webmaster wants to force a refresh the contents, -he just removes page.html (usually done by a cronjob). +page.html. Once it was run, the server sends out the data of +page.html. When the webmaster wants to force a refresh the contents, +he just removes page.html (usually done by a cronjob). @@ -1421,7 +1421,7 @@ our editor? Impossible?

No! We just combine the MIME multipart feature, the webserver NPH feature and the URL manipulation power of mod_rewrite. First, we establish a new URL -feature: Adding just :refresh to any URL causes this to be refreshed +feature: Adding just :refresh to any URL causes this to be refreshed every time it gets updated on the filesystem.

@@ -1557,7 +1557,7 @@ exit(0);
 
Description:
-The <VirtualHost> feature of Apache is nice and works great +The <VirtualHost> feature of Apache is nice and works great when you just have a few dozens virtual hosts. But when you are an ISP and have hundreds of virtual hosts to provide this feature is not the best choice. @@ -1640,14 +1640,14 @@ RewriteRule ^/(.*)$ %1/$1 [E=VHOST:${lowercase:%{HTTP_HOST}}]
Description:
How can we block a really annoying robot from retrieving pages of a specific -webarea? A /robots.txt file containing entries of the "Robot +webarea? A /robots.txt file containing entries of the "Robot Exclusion Protocol" is typically not enough to get rid of such a robot.

Solution:
We use a ruleset which forbids the URLs of the webarea -/~quux/foo/arc/ (perhaps a very deep directory indexed area where the +/~quux/foo/arc/ (perhaps a very deep directory indexed area where the robot traversal would create big server load). We have to make sure that we forbid access only to the particular robot, i.e. just forbidding the host where the robot runs is not enough. This would block users from this host, @@ -1655,9 +1655,9 @@ too. We accomplish this by also matching the User-Agent HTTP header information.

-RewriteCond %{HTTP_USER_AGENT}   ^NameOfBadRobot.*      
-RewriteCond %{REMOTE_ADDR}       ^123\.45\.67\.[8-9]$
-RewriteRule ^/~quux/foo/arc/.+   -   [F]
+RewriteCond %{HTTP_USER_AGENT}   ^NameOfBadRobot.*      
+RewriteCond %{REMOTE_ADDR}       ^123\.45\.67\.[8-9]$
+RewriteRule ^/~quux/foo/arc/.+   -   [F]
 

@@ -1682,15 +1682,15 @@ can at least restrict the cases where the browser sends a HTTP Referer header.

-RewriteCond %{HTTP_REFERER} !^$                                  
+RewriteCond %{HTTP_REFERER} !^$                                  
 RewriteCond %{HTTP_REFERER} !^http://www.quux-corp.de/~quux/.*$ [NC]
-RewriteRule .*\.gif$        -                                    [F]
+RewriteRule .*\.gif$        -                                    [F]
 

 RewriteCond %{HTTP_REFERER}         !^$                                  
 RewriteCond %{HTTP_REFERER}         !.*/foo-with-gif\.html$
-RewriteRule ^inlined-in-foo\.gif$   -                        [F]
+RewriteRule ^inlined-in-foo\.gif$   -                        [F]
 
@@ -1760,19 +1760,19 @@ the Apache proxy?

Solution:
We first have to make sure mod_rewrite is below(!) mod_proxy in the -Configuration file when compiling the Apache webserver. This way it +Configuration file when compiling the Apache webserver. This way it gets called _before_ mod_proxy. Then we configure the following for a host-dependend deny...

-RewriteCond %{REMOTE_HOST} ^badhost\.mydomain\.com$ 
+RewriteCond %{REMOTE_HOST} ^badhost\.mydomain\.com$ 
 RewriteRule !^http://[^/.]\.mydomain.com.*  - [F]
 

...and this one for a user@host-dependend deny:

-RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST}  ^badguy@badhost\.mydomain\.com$
+RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST}  ^badguy@badhost\.mydomain\.com$
 RewriteRule !^http://[^/.]\.mydomain.com.*  - [F]
 
@@ -1796,9 +1796,9 @@ when using the Basic Auth via mod_access). We use a list of rewrite conditions to exclude all except our friends:

-RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend1@client1.quux-corp\.com$ 
-RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend2@client2.quux-corp\.com$ 
-RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend3@client3.quux-corp\.com$ 
+RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend1@client1.quux-corp\.com$ 
+RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend2@client2.quux-corp\.com$ 
+RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend3@client3.quux-corp\.com$ 
 RewriteRule ^/~quux/only-for-friends/      -                                 [F]
 
@@ -1872,8 +1872,8 @@ to put the resulting (usually rewritten) URL on STDOUT (same order!).

 RewriteEngine on
-RewriteMap    quux-map       prg:/path/to/map.quux.pl
-RewriteRule   ^/~quux/(.*)$  /~quux/${quux-map:$1}
+RewriteMap    quux-map       prg:/path/to/map.quux.pl
+RewriteRule   ^/~quux/(.*)$  /~quux/${quux-map:$1}
 

@@ -1893,9 +1893,9 @@ while (<>) {
 
 

This is a demonstration-only example and just rewrites all URLs -/~quux/foo/... to /~quux/bar/.... Actually you can program -whatever you like. But notice that while such maps can be used also by -an average user, only the system administrator can define it. +/~quux/foo/... to /~quux/bar/.... Actually you can program +whatever you like. But notice that while such maps can be used also by +an average user, only the system administrator can define it.