| |
|
|
Apache 1.3
URL Rewriting Guide
Host Deny
- Description:
- How can we forbid a list of externally configured hosts from using our server?
- Solution:
- For Apache >= 1.3b6:
RewriteEngine on
RewriteMap hosts-deny txt:/path/to/hosts.deny
RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND [OR]
RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND
RewriteRule ^/.* - [F]
|
For Apache <= 1.3b6:
RewriteEngine on
RewriteMap hosts-deny txt:/path/to/hosts.deny
RewriteRule ^/(.*)$ ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND}/$1
RewriteRule !^NOT-FOUND/.* - [F]
RewriteRule ^NOT-FOUND/(.*)$ ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND}/$1
RewriteRule !^NOT-FOUND/.* - [F]
RewriteRule ^NOT-FOUND/(.*)$ /$1
|
##
## hosts.deny
##
## ATTENTION! This is a map, not a list, even when we treat it as such.
## mod_rewrite parses it for key/value pairs, so at least a
## dummy value "-" must be present for each entry.
##
193.102.180.41 -
bsdti1.sdm.de -
192.76.162.40 -
|
URL-Restricted Proxy
- Description:
- How can we restrict the proxy to allow access to a configurable set of internet sites
only? The site list is extracted from a prepared bookmarks file.
- Solution:
- We first have to make sure mod_rewrite is below(!) mod_proxy in the
Configuration
file when compiling the Apache webserver (or in the AddModule list of httpd.conf
in the case of dynamically loaded modules), as it must get called _before_
mod_proxy.
For simplicity, we generate the site list as a textfile map (but see the
mod_rewrite
documentation for a conversion script to DBM format). A typical Netscape bookmarks
file can be converted to a list of sites with a shell script like this:
#!/bin/sh
cat ${1:-~/.netscape/bookmarks.html} |
tr -d '\015' | tr '[A-Z]' '[a-z]' | grep href=\" |
sed -e '/href="file:/d;' -e '/href="news:/d;' \
-e 's|^.*href="[^:]*://\([^:/"]*\).*$|\1 OK|;' \
-e '/href="/s|^.*href="\([^:/"]*\).*$|\1 OK|;' |
sort -u
|
We redirect the resulting output into a text file called goodsites.txt.
It now looks similar to this:
www.apache.org OK
xml.apache.org OK
jakarta.apache.org OK
perl.apache.org OK
...
|
We reference this site file within the configuration for the VirtualHost
which is responsible for serving as a proxy (often not port 80, but 81, 8080 or 8008).
<VirtualHost *:8008>
...
RewriteEngine On
# Either use the (plaintext) allow list from goodsites.txt
RewriteMap ProxyAllow txt:/usr/local/apache/conf/goodsites.txt
# Or, for faster access, convert it to a DBM database:
#RewriteMap ProxyAllow dbm:/usr/local/apache/conf/goodsites
# Match lowercased hostnames
RewriteMap lowercase int:tolower
# Here we go:
# 1) first lowercase the site name and strip off a :port suffix
RewriteCond ${lowercase:%{HTTP_HOST}} ^([^:]*).*$
# 2) next look it up in the map file.
# "%1" refers to the previous regex.
# If the result is "OK", proxy access is granted.
RewriteCond ${ProxyAllow:%1|DENY} !^OK$ [NC]
# 3) Disallow proxy requests if the site was _not_ tagged "OK":
RewriteRule ^proxy: - [F]
...
</VirtualHost>
|
Proxy Deny
- Description:
- How can we forbid a certain host or even a user of a special host from using 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 gets called _before_
mod_proxy. Then we configure the following for a host-dependend deny...
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$
RewriteRule !^http://[^/.]\.mydomain.com.* - [F]
|
Special Authentication Variant
- Description:
- Sometimes a very special authentication is needed, for instance a authentication which
checks for a set of explicitly configured users. Only these should receive access and
without explicit prompting (which would occur when using the Basic Auth via mod_access).
- Solution:
- 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$
RewriteRule ^/~quux/only-for-friends/ - [F]
|
Referer-based Deflector
- Description:
- How can we program a flexible URL Deflector which acts on the "Referer" HTTP
header and can be configured with as many referring pages as we like?
- Solution:
- Use the following really tricky ruleset...
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]
|
... in conjunction with a corresponding rewrite map:
##
## deflector.map
##
http://www.badguys.com/bad/index.html -
http://www.badguys.com/bad/index2.html -
http://www.badguys.com/bad/index3.html http://somewhere.com/
|
This automatically redirects the request back to the referring page (when
"-" is used as the value in the map) or to a specific URL (when an URL is
specified in the map as the second argument).
Other
External Rewriting Engine
- Description:
- A FAQ: How can we solve the FOO/BAR/QUUX/etc. problem? There seems no solution by the
use of mod_rewrite...
- Solution:
- Use an external rewrite map, i.e. a program which acts like a rewrite map. It is run
once on startup of Apache receives the requested URLs on STDIN and has 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}
|
#!/path/to/perl
# disable buffered I/O which would lead
# to deadloops for the Apache server
$| = 1;
# read URLs one per line from stdin and
# generate substitution URL on stdout
while (<>) {
s|^foo/|bar/|;
print $_;
}
|
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.
|
|
|
|
|
|
© 2005 Active-Venture.com Web
Page Hosting
Service
|
|
|
|

|
|
< All parts should go together without forcing. You must remember that the parts you are reassembling were disassembled by you. Therefore, if you can't get them together again, there must be a reason. By all means, do not use a hammer.
> |
|
|
| |
|
Disclaimer: This
documentation is provided only for the benefits of our hosting customers.
For authoritative source of the documentation, please refer to http://httpd.apache.org/docs/
|
|
|