logo
Apache Lounge
Webmasters

 


About

Forum Index Downloads Search Register Log in  RSS Apache Lounge
 



Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Apache Lounge is not sponsored.

Your donations will help to keep this site alive and well, and continuing building binaries.



mod_vhost_dbd, environment variables and rewriterule

 
Post new topic   Reply to topic    Apache Forum Index -> Apache third-party Modules



View previous topic :: View next topic  
Author Message
Baardmans



Joined: 02 Feb 2012
Posts: 7
Location: Holland

PostPosted: Thu 02 Feb '12 12:45    Post subject: mod_vhost_dbd, environment variables and rewriterule Reply with quote

Hey everyone,

First off all, I would like to thank you all for the great downloads and information on this website! I've been having great fun learning and setting up an Apache server that will host many domains that all use our custom cms.

Now for my question. This is the situation:
We have a lot of the following virtual hosts defined in the vhosts config file of Apache.
Code:
<VirtualHost xx.xx.xx.xx:80>
   ServerName xxx.com
   DocumentRoot C:\xxxx
   ErrorDocument 500 "/system/error/500.php"
   ErrorDocument 401 "/system/error/401.php"
   ErrorDocument 403 "/system/error/403.php"
   ErrorDocument 404 "/system/error/404.php"
</VirtualHost>

but also domains that redirect to the above virtual host:
Code:
<VirtualHost xx.xx.xx.xx:80>
   Servername yyy.com
   ServerAlias zzz.com
   ServerAlias rrr.com
   Redirect permanent / http://xxx.com
</VirtualHost>


Problem:
Thanks to mod_vhost_dbd I no longer have to put the first virtual host in the vhosts file, I've already got this working and it works great! However there is no out of the box solution for the second vhost.

Solution?:
However i've read the mod_vhost_dbd documentation carefully and read the following sentence:
Quote:
For Apache 2.2.9+, any additional columns which are returned will set an environment variable with the same name as the column name. If the column value is NULL, any existing environment variable which matches the column name will be unset.


So I wanted to stretch the mod_vhost_dbd module a bit and try to get it to return an environment variable 'docredirect' and use this in a rewritecond/rule where "if this env variable is not empty redirect a person to the specified url in the variable".

Problem in solution:
It seems that either mod_vhost_dbd does not return the env variable -I've checked the code of mod_vhost_dbd without any knowledge and it seems to be there- Or rewritecond/rule does not accept the env variable set by mod_vhost_dbd. Which seems to be common problem with setenv/rewriterule (https://www.google.nl/#sclient=psy-ab&hl=nl&source=hp&q=rewriterule+setenv&pbx=1&oq=rewriterule+setenv&aq=f&aqi=g-vL2&aql=&gs_sm=e&gs_upl=440l1909l2l2148l5l1l2l2l2l0l151l151l0.1l5l0&bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&fp=b006212f803cb94c&biw=1680&bih=949). I was hoping that mod_vhost_dbd executes before rewriterule however as it is now, it doesn't seem work.

This is the code for now, purely as a test case which does not work. I've tried many variations for the variable such as %{docredirect} and uppercasing everything.

Quote:
<VirtualHost xx.xx.xx.xx:80>
ServerName *
DocumentRoot "D:/web/test/"

DBDriver odbc
DBDParams "DATASOURCE=apache"
DBDocRoot "SELECT docroot, docredirect FROM vhosts WHERE dochost = %s" HOSTNAME

#ProxyPassReverse / http://%{HTTP_HOST}:8080/
RewriteEngine on
RewriteRule ^/$ http://www.%{ENV:docredirect} [R=permanent,L]
#RewriteCond %{REQUEST_URI} !.*\.(gif|png|jpg|bmp|pdf|swf|txt|js|ico|css|mp3)$
#RewriteRule ^/(.*) http://%{HTTP_HOST}:8080/$1 [P]
</VirtualHost>


I'm using Apache 2.2.21 with mod_vhosts_dbd from this website.

Hopefully i'm just doing something wrong or maybe there is another solution that is just as flexible for the second vhost problem? I've checked how hard it would be to add something like this in a good manner for the module but it seems it would require some time to get the know the whole Apache APR environment and c++ Wink The mod_vhost_dbd module is perfect for us as it allows us to write a custom application that governs these rules.

Thanks!
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 610
Location: Milford, MA, USA

PostPosted: Fri 03 Feb '12 17:29    Post subject: Reply with quote

I think your problem may be that when Apache is working inside a <VirtualHost> directive, and it redirects to a different host (e.g. it redirects http://zzz.com to http://xxx.com) - then Apache never looks up any actual filenames. Therefore, it doesn't care about filesystem paths, it never uses DocumentRoot, and it never calls mod_vhost_dbd.

You might give this a try:
    Remove all your <VirtualHost> directives. In other words, don't use any Virtual hosts, or a vhosts config file, at all in your configuration.
    Put your mod_rewite directives inside the <Directory /> block - so they apply to all possible paths, like this
    Code:
    <Directory />
    RewriteEngine on
    RewriteCond %{ENV:docredirect} .+
    RewriteRule ^(.*) http://%{ENV:docredirect}/$1 [R=permanent,L]
    ...
    </Directory>


This should cause the RewriteRule to fire whenever the environment variable docredirect is one-or-more characters long.
Your SQL statement should return "/" for docroot, and the new host name (e.g. "xxx.com") for docredirect, whenever you want to redirect one host to another.

I haven't tested this - so I am not really sure if it will do what you want, but it seems worth a try.

The What is matched? section in RewriteRule explains when Apache cares about filesystem paths (so it will call mod_vhost_dbd), and when it doesn't care about filesystem paths.

I hope this helps. Please let us know if it works for you.
-tom-
Back to top
Baardmans



Joined: 02 Feb 2012
Posts: 7
Location: Holland

PostPosted: Sun 05 Feb '12 1:32    Post subject: Reply with quote

Absolutely amazing, i had already given up but this works like a charm!
Thank you very much for the help and the reference, i'll have a look at it Smile

As suggested the code became the following:
Code:
DBDriver odbc
DBDParams "DATASOURCE=apache"
DBDocRoot "SELECT docroot,docredirect FROM vhosts WHERE dochost = %s" HOSTNAME

<Directory "D:/Web">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all
   
   RewriteEngine on
   RewriteCond %{ENV:docredirect} .+
   RewriteRule ^(.*) %{ENV:docredirect} [R=permanent,L]

</Directory>


cheers!
Back to top


Post new topic   Reply to topic    Apache Forum Index -> Apache third-party Modules
Page 1 of 1