# apache/conf.d/vhosts.conf using mod-rewrite and cgiwrap constructively
#
# Search terms: apache vhost rewrite map cgiwrap php cgi file permissions nonstop ISP
#
# We are doing 2 things here
#
# We have many virtual hosts on our shared web-servers. Restarting apache
# as each vhost is added or removed is not acceptable, so a method is needed to
# simplify the virtual hosting for continuous operation
#
# We therefore host www.foo.dom in /Sites/dom/foo/www
# The account is foo.dom, and the home directory /Sites/dom/foo
# if foo.dom would start with a number we prefix 'x' eg
# 42istheanswer.com -> x42istheanswer.com for the account name
# by having a simple map like this apache can run for over a year without
# requiring restart (other than for patches)
#
# That's half the complexity. the other half comes from running PHP, perl, cgi
# as the user, rather than as apache, so the file/directory permissions are right,
# quotas observed etc. We depend on cgiwrap to do this for us
# albeit a very slightly tweaked cgiwrap, which also is configured to run cgi-php
#
# $Copyright: (c) 2001 Andrew Macpherson, OA5.com Limited $
#
# The ideas here may be freely used, at the user's own risk
#
NameVirtualHost *
<VirtualHost *>
DocumentRoot /Sites
ScriptAlias /cgi-bin/ /etc/httpd/cgi-bin/
Alias /error/ /var/www/error/
RewriteEngine On
RewriteLog /etc/httpd/logs/mod_rewrite.log
RewriteLogLevel 0
RewriteMap lowercase int:tolower
# keep the main system CGI bin intact
RewriteCond %{REQUEST_URI} !^/cgi-bin\/cgiwrap
RewriteCond %{REQUEST_URI} !^/cgi-bin\/secure\/cgiwrap
# make the requested vhost lowercase
RewriteCond ${lowercase:%{HTTP_HOST}} ^(([a-z0-9][-a-z0-9]+)\.)+([a-z0-9][-a-z0-9]+)$
RewriteRule ^(.+) ${lowercase:%{HTTP_HOST}}$1 [C]
#
# So we can test the moved hosts in advance with the same config file on live and
# test servers (which may be different for different vhosts, just by playing with
# DNS entries
#
RewriteRule ^shadow\.(.*)$ $1
RewriteRule ^proof\.(.*)$ www.$1
RewriteRule ^test\.(.*)$ www.$1
#
# Per 'user' cgi-bin really vhost --- weirdness to get the username by oa5 rules
#
# Note the second group in the regexp can contain multiple domain components
#
RewriteRule ^([^/\.]+)\.([0-9][^/]+)\.([^/\.]+)\/cgi-bin/(.*)$ /cgi-bin/cgiwrap/x$2.$3/cgi-bin/$4 [T=application/x-httpd-cgi,PT,L]
RewriteRule ^([^/\.]+)\.([0-9][^/]+)\.([^/\.]+)\/cgi-bind/(.*)$ /cgi-bin/cgiwrapd/x$2.$3/cgi-bin/$4 [T=application/x-httpd-cgi,PT,L]
#
RewriteRule ^([^/\.]+)\.([0-9][^/]+)\.([^/\.]+)\/nph-cgi-bin/(.*)$ /cgi-bin/nph-cgiwrap/x$2.$3/cgi-bin/$4 [T=application/x-httpd-cgi,PT,L]
RewriteRule ^([^/\.]+)\.([0-9][^/]+)\.([^/\.]+)\/nph-cgi-bind/(.*)$ /cgi-bin/nph-cgiwrapd/x$2.$3/cgi-bin/$4 [T=application/x-httpd-cgi,PT,L]
#
RewriteRule ^([^/\.]+)\.([^/\.]+)/cgi-bin/(.*)$ /cgi-bin/cgiwrap/$1.$2/cgi-bin/$3 [T=application/x-httpd-cgi,PT,L]
RewriteRule ^([^/\.]+)\.([^/]+)\.([^/\.]+)\/cgi-bin/(.*)$ /cgi-bin/cgiwrap/$2.$3/cgi-bin/$4 [T=application/x-httpd-cgi,PT,L]
#
RewriteRule ^([^/\.]+)\.([^/\.]+)/cgi-bind/(.*)$ /cgi-bin/cgiwrapd/$1.$2/cgi-bin/$3 [T=application/x-httpd-cgi,PT,L]
RewriteRule ^([^/\.]+)\.([^/]+)\.([^/\.]+)\/cgi-bind/(.*)$ /cgi-bin/cgiwrapd/$2.$3/cgi-bin/$4 [T=application/x-httpd-cgi,PT,L]
#
RewriteRule ^([^/\.]+)\.([^/\.]+)/nph-cgi-bin/(.*)$ /cgi-bin/nph-cgiwrap/$1.$2/cgi-bin/$3 [T=application/x-httpd-cgi,PT,L]
RewriteRule ^([^/\.]+)\.([^/]+)\.([^/\.]+)\/nph-cgi-bin/(.*)$ /cgi-bin/nph-cgiwrap/$2.$3/cgi-bin/$4 [T=application/x-httpd-cgi,PT,L]
#
RewriteRule ^([^/\.]+)\.([^/\.]+)/nph-cgi-bind/(.*)$ /cgi-bin/nph-cgiwrapd/$1.$2/cgi-bin/$3 [T=application/x-httpd-cgi,PT,L]
RewriteRule ^([^/\.]+)\.([^/]+)\.([^/\.]+)\/nph-cgi-bind/(.*)$ /cgi-bin/nph-cgiwrapd/$2.$3/cgi-bin/$4 [T=application/x-httpd-cgi,PT,L]
#
# Now remap the real files
#
RewriteRule ^([^/]+)(/icons/.*) $2 [L,PT]
RewriteRule ^([^/]+)(/error/.*) $2 [L,PT]
#
# Make user executed stuff in the web tree (vs cgi-bin) also run under cgiwrap
# only support php and cgi extensions for this, pl should be in their cgi-bin dir
#
RewriteRule ^([^/\.]+)\.([^/\.]+)(/.*)(.php|.cgi|.php[34])$ /cgi-bin/cgiwrap/$1.$2/Sites/$2/$1$3$4 [T=application/x-httpd-cgi,PT,L]
RewriteRule ^([^/\.]+)\.([^/\.]+)\.([^/\.]+)(/.*)(.php|.cgi|.php[34])$ /cgi-bin/cgiwrap/$2.$3/Sites/$3/$2/$1$4$5 [T=application/x-httpd-cgi,PT,L]
RewriteRule ^([^/\.]+)\.([^/\.]+)\.([^/\.]+)\.([^/\.]+)(/.*)(.php|.cgi|.php[34])$ /cgi-bin/cgiwrap/$2.$3.$4/Sites/$4/$3/$2/$1$5$6 [T=application/x-httpd-cgi,PT,L]
RewriteRule ^([^/\.]+)\.([^/\.]+)\.([^/\.]+)\.([^/\.]+)\.([^/\.]+)(/.*)(.php|.cgi|.php[34])$ /cgi-bin/cgiwrap/$2.$3.$4.$5/Sites/$5/$4/$3/$2/$1$6$7 [T=application/x-httpd-cgi,PT,L]
#
# Simple host to directory mapping
#
RewriteRule ^([^/\.]+)\.([^/\.]+)(/.*) /Sites/$2/$1$3 [L]
RewriteRule ^([^/\.]+)\.([^/\.]+)\.([^/\.]+)(/.*) /Sites/$3/$2/$1$4 [L]
RewriteRule ^([^/\.]+)\.([^/\.]+)\.([^/\.]+)\.([^/\.]+)(/.*) /Sites/$4/$3/$2/$1$5 [L]
RewriteRule ^([^/\.]+)\.([^/\.]+)\.([^/\.]+)\.([^/\.]+)\.([^/\.]+)(/.*) /Sites/$5/$4/$3/$2/$1$6 [L]
</VirtualHost>