Setting ENV variable based on a URL match.

New to mod_rewrite? This is a good place to start.

Setting ENV variable based on a URL match.

Postby nkj » Wed Sep 23, 2009 1:47 pm

Hello,

I've currently got a vhost record that sets an ENV variable which is used by the webapp. However there is a possibility of a second instance of the same webapp in a subdirectory. If this is the case, I need the ENV variable to be set for this secondary webapp instead of the default one.

For example:
Code: Select all
<VirtualHost *:80>
    ServerName foo.com

    Alias /bar /var/www/bar

    DocumentRoot /var/www/foo
    ErrorLog /var/log/httpd/foo-error_log
    CustomLog /var/log/httpd/foo-access_log combined
    DirectoryIndex index.html index.htm index.php index.php4 index.php5

    Options +FollowSymlinks -Indexes +ExecCGI
    SetEnv CONF foo
</VirtualHost>


So, if the URL entered is just 'foo.com' the CONF variable will be set to 'foo'. However if the URL is 'foo.com/bar', I'd like the CONF variable to be reset to 'bar'.

How can I do this?

Alternately, perhaps a more robust way would be to have two vhost directives. (one for foo.com one for bar.com) and then someone alias foo.com/bar to bar.com (so from the end users perspective they are at foo.com/bar but apache is actually using the settings defined in the vhost entry for bar.com. This is basically like using an HTML iframe, but cleaner, and in the apacheconf. Is there a way to do this?

Thanks,
Nick
nkj
 
Posts: 21
Joined: Fri May 15, 2009 11:32 am

Postby richardk » Fri Sep 25, 2009 12:14 pm

SetEnvIf should work
Code: Select all
    SetEnv CONF foo
    SetEnvIfNoCase REQUEST_URI ^/bar(/.*)?$ CONF bar


Alternately, perhaps a more robust way would be to have two vhost directives. (one for foo.com one for bar.com) and then someone alias foo.com/bar to bar.com (so from the end users perspective they are at foo.com/bar but apache is actually using the settings defined in the vhost entry for bar.com. This is basically like using an HTML iframe, but cleaner, and in the apacheconf. Is there a way to do this?

As far as i know you can't do that. Or at least, you can't use the configuration from a different <VirtualHost> for a sub directory. You might be able to use configuration in a <Directory> or <Location> instead.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 25 guests

cron