httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eric Anderson <e...@afaik.us>
Subject [users@httpd] mod_rewire questions: Environment variables
Date Wed, 08 Dec 2004 22:14:19 GMT
I have a mod_rewrite problem that I cannot seem to quite work out. Here 
is the basic situation. I am hosting a few website for friends and 
wanted to setup the server to be database driven instead of config 
driven. The goal is so that I can add a new site by simply creating a 
couple records in a SQL database and have no apache restart. To 
accomplish that in Apache I am using mod_rewrite. My basic setup is the 
following (try to ignore the stupid line wraps):

**** RELEVANT PORTIONS OF APACHE.CONF ****

RewriteLock /tmp/rewrite.lck
<VirtualHost 66.220.1.98>
ServerName www.defaulthost.com
DocumentRoot /web/notfound.realsimplehosting.com

RewriteEngine on
#RewriteLog /tmp/rewrite.log
#RewriteLogLevel 9
RewriteMap webrootdir prg:/home/realsimple/projects/rsh/bin/webroot_lookup

# Subversion Support
RewriteCond %{HTTP_HOST} ^svn\.
RewriteCond %{REQUEST_URI} ^/$
RewriteRule / ${webrootdir:%{HTTP_HOST}}/ [R=permanent,L]

# Webmail Support
RewriteCond %{HTTP_HOST} ^email\.
RewriteCond %{REQUEST_URI} ^/$
RewriteRule / /cgi-bin/sqwebmail [R=permanent,L]

# CGI-BIN support
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteRule ^/cgi-bin/(.*)$ 
${webrootdir:%{HTTP_HOST}}/cgi-bin/$1[E=DOCUMENT_ROOT:${webrootdir:%{HTTP_HOST}}/www/,E=SQWEBMAIL_TEMPLATEDIR=${webrootdir:bypass.%{HTTP_HOST}}/email,L]

# Error handling
RewriteCond %{REQUEST_URI} ^/error/
RewriteRule ^/error/(.*)$ /var/www/error/$1 
[E=DOCUMENT_ROOT:${webrootdir:%{HTTP_HOST}}/www/,L]

# Any other request
RewriteRule ^/(.*)$ ${webrootdir:%{HTTP_HOST}}/www/$1 
[E=DOCUMENT_ROOT:${webrootdir:%{HTTP_HOST}}/www/,E=SQWEBMAIL_TEMPLATEDIR=${webrootdir:bypass.%{HTTP_HOST}}/email]

**** RELEVANT PORTIONS OF APACHE CONF ****

It looks a bit complex but it isn't too bad. I put everything inside a 
virtual host so that it would use this virtual host as a default. That 
way if I had something really screwy I could just create a new virtual 
host after this virtual host defination to overwrite the database stuff. 
So far it has been flexible enough to not need that. Must of the stuff 
is pretty standard:

* We give it a rewrite lock file since we are using a program to do the 
lookup.
* I get it a default servername and document root but they should never 
really be used.
* I turn the rewrite engine on and tell it where my rewrite program is
* I then have a couple of sections for special case programs.
   + The SVN Support will do a client redirect to the specified 
repository. This way if they type http://svn.foo.com they get redirected 
to http://svn.foo.com/foo.com.
   + The webmail support simply does a client redirect to redirect all 
requests for http://email.foo.com to 
http://email.foo.com/cgi-bin/sqwebmail so the user can be a bit lazier.
   + The CGI-BIN support simply allows access to the users cgi-bin 
directory instead of their web (htdocs) directory.
   + The error handling gets the apache default error messages (I 
eventually want to make this per-site).
* After all that it handles the general case of directing the page to 
the right directory.

This solution is similar to some the mass virtual hosting solutions out 
there listed on the apache site but it is database run and can be 
massages plenty by my script (which is written in ruby).

So on to my problem. Everything is working fine except my environment 
variables. They do not seem to be getting set properly. I don't really 
care about the DOCUMENT_ROOT one much except that I wanted to ensure 
scripts that rely on DOCUMENT_ROOT environment variable get the correct 
root directory for their website will work. But the one I am currently 
after is the SQWEBMAIL_TEMPLATES environment variable. I am using 
sqwebmail for my web-based email and it allows you to have different 
HTML templates depending on the environment variable. I want to be able 
to go to: email.foo.com and see different templates than if I go to 
email.bar.com. It looks like it is setup correctly except that it 
doesn't work. :) So I made a quick page to test the environment 
variables. I created a page called test.shtml under a site called 
account.realsimplehosting.com. The page consist of the following:

<html>
<body>
The document root is: <!--#echo var="DOCUMENT_ROOT"--><br/>
The sqwebmail dir is: <!--#echo var="SQWEBMAIL_TEMPLATEDIR"-->
</body>
</html>	

The output I receive is:

The document root is: /web/notfound.realsimplehosting.com
The sqwebmail dir is: (none)

This means that the sqwebmail template directly is not getting set and 
the document root is sticking to the default for the virtual host. If I 
remove the document root line from my apache.conf I get some default 
directory (/home/htdocs I think). I tried to debug this problem by 
turning on mod_rewrite logging and put it at level 9. The bottom of this 
email is what I got. You can clearly see it says it is setting the 
document root but obviously it is not (or what is set is getting 
overwritten). You can also see that it does the lookup for sqwebmail but 
never sets the environment variable.

So my question is what am I doing wrong? My primary goal is to get 
sqwebmail's template variable set but it would be nice for the Document 
root to be correct also. Any suggestions?

64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (2) 
init rewrite engine with requested uri /test.shtml
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (3) 
applying pattern '/' to uri '/test.shtml'
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (4) 
RewriteCond: input='account.realsimplehosting.com' pattern='^svn\.' => 
not-matched
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (3) 
applying pattern '/' to uri '/test.shtml'
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (4) 
RewriteCond: input='account.realsimplehosting.com' pattern='^email\.' => 
not-matched
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (3) 
applying pattern '^/cgi-bin/(.*)$' to uri '/test.shtml'
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (3) 
applying pattern '^/error/(.*)$' to uri '/test.shtml'
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (3) 
applying pattern '^/(.*)$' to uri '/test.shtml'
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (5) map 
lookup OK: map=webrootdir key=account.realsimplehosting.com -> 
val=/virtual/1/web/account.realsimplehosting.com
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (2) 
rewrite /test.shtml -> 
/virtual/1/web/account.realsimplehosting.com/www/test.shtml
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (5) map 
lookup OK: map=webrootdir key=account.realsimplehosting.com -> 
val=/virtual/1/web/account.realsimplehosting.com
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (5) 
setting env variable 'DOCUMENT_ROOT' to 
'/virtual/1/web/account.realsimplehosting.com/www/'
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (5) map 
lookup OK: map=webrootdir key=bypass.account.realsimplehosting.com -> 
val=/virtual/1/web/account.realsimplehosting.com
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (2) 
local path result: 
/virtual/1/web/account.realsimplehosting.com/www/test.shtml
64.30.162.221 - - [08/Dec/2004:16:26:50 --0500] 
[account.realsimplehosting.com/sid#81ba158][rid#86f3f68/initial] (1) 
go-ahead with 
/virtual/1/web/account.realsimplehosting.com/www/test.shtml [OK]

Mime
View raw message