LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   Trying to figure out Apache mod_rewrite and environment variables (http://www.linuxquestions.org/questions/linux-server-73/trying-to-figure-out-apache-mod_rewrite-and-environment-variables-881290/)

ajs418 05-17-2011 05:16 PM

Trying to figure out Apache mod_rewrite and environment variables
 
To all the Apache experts out there...

I'm trying to configure my httpd.conf file so that I flip an environment variable or comment out a single line at the top of an httpd.conf (it's a proxy server) and "enable" redirects in various Location directives.

In the following example I have two proxy locations (app1 and app2). App1 is under maintenance and I'd like to quickly flip this one line item to force it to redirect to a maintenance page. This does not work...

Code:

<VirtualHost *:port>
RewriteEngine On
SetEnvIf Request_URI "app1" app1maint=yes
#SetEnvIf Request_URI "app2" app1maint=yes

<Location /app1>
RewriteCond %{ENV:app1maint} ^yes$
RewriteRule (.*) http://host:port/app1MaintenancePage.html
</Location>

<Location /app2>
RewriteCond %{ENV:app2maint} ^yes$
RewriteRule (.*) http://host:port/app2MaintenancePage.html
</Location>
</VirtualHost>

This whole thing does work if I put all the Rewrite and Environment directives into the Location directives for each app/location, but that defeats the purpose of this because now an admin has to muck around the httpd.conf file and comment out each line. We have many locations servicing a number of applications so the file can be quite large.

carltm 05-21-2011 07:55 AM

If I were doing this, I would create a set of files, one for
each configuration. Then put in an include statement in the
httpd.conf that points to the current configuration.

For example, create a file named "app-normal.conf" with both
apps enabled. Create one named "app-1maint.conf" set to enable
only app2. Finally create one named "app-2maint.conf" set to
enable only app1.

Next I would create a soft link from whichever configuration
you want to enable. For example "ln -s app-normal.conf app.conf".
You would then remove the VirtualHost settings from the httpd.conf
file, and replace them with "Include app.conf". Restart the
web service and make sure it works.

After this initial setup, all you need to do to change modes
is run "ln -sf app-???.conf app.conf" and restart the webserver.

I hope my explanation makes sense. Ask if you have any questions!

ajs418 05-30-2011 11:27 AM

Thanks for the answer, and you're response definitely makes perfect sense. The only downside is that we've got about 4 applications being serviced by this proxy (maybe a few more in the future), so all the combinations and permutations of apps being up or down could result in all sorts of different conf files.

I ended up just putting a single rewrite rule per application at the beginning of the vhost directive (before all the Location directives) so it is just a matter of uncommenting one line to put up the "maintenance" page.

Thanks for your suggestion!

Quote:

Originally Posted by carltm (Post 4362800)
If I were doing this, I would create a set of files, one for
each configuration. Then put in an include statement in the
httpd.conf that points to the current configuration.

For example, create a file named "app-normal.conf" with both
apps enabled. Create one named "app-1maint.conf" set to enable
only app2. Finally create one named "app-2maint.conf" set to
enable only app1.

Next I would create a soft link from whichever configuration
you want to enable. For example "ln -s app-normal.conf app.conf".
You would then remove the VirtualHost settings from the httpd.conf
file, and replace them with "Include app.conf". Restart the
web service and make sure it works.

After this initial setup, all you need to do to change modes
is run "ln -sf app-???.conf app.conf" and restart the webserver.

I hope my explanation makes sense. Ask if you have any questions!


carltm 06-03-2011 07:22 AM

Just a thought...your httpd.conf file is probably pretty large and it
might be simpler to comment a line if you put the 4 rewrite rules in
a separate file (named maintenance.conf?) and then put in a line to
include this file.

This would be especially helpful if you have multiple people who may
need to enable or disable the apps.


All times are GMT -5. The time now is 02:55 PM.