How can I get a conditional .htaccess block?

This is an Apache question that you have probably come across before. I want to have one source package that I can deploy on my workstation, on my intermediate server and on my production server, but to load different .htaccess options depending on what the URL was.

Please note that I used kludge with an IfModule call, but this will not work with our new production server, because it has all the same modules as my intermediate server.

Note. I need to associate SetEnv with these rewrites. Currently, if you use a RewriteCond, it is only associated with the next RewriteRule, but not with SetEnv at the bottom.

+15
apache .htaccess apache2
Apr 10 '09 at 2:23
source share
3 answers

Instead of using SetEnv, use the options for setting the environment variable of the RewriteRule itself:

RewriteCond %{HTTP_HOST} =foo.com RewriteRule ^ - [E=VARNAME:foo] RewriteCond %{HTTP_HOST} =bar.com RewriteRule ^ - [E=VARNAME:bar] 

Although I prefer to do such things by passing flags to the httpd process at startup and looking for them using IfDefine blocks.

 <IfDefine FOO> SetEnv VARNAME foo </IfDefine> <IfDefine BAR> SetEnv VARNAME bar </IfDefine> 
+15
Apr 21 '09 at 18:07
source share

On Ubuntu Linux , the IfDefine variable IfDefine set to

 /etc/apache2/envvars 

and is called APACHE_ARGUMENTS . So, at the bottom of this file I had to add:

 export APACHE_ARGUMENTS="-D dev" 

... and then bounce off the server:

 /etc/init.d/apache2 stop /etc/init.d/apache2 start 

In other systems:

However, there is a Debian article on this topic that discusses the following here . In this example, the editing file is / etc / default / apache2, and the variable is called APACHE_DEFINES .

Similarly, on some systems, this is a variable called OPTIONS , which is set to /etc/sysconfig/httpd .

So, you really need to find the start section in the apache2ctl file. So, start with whereis apache2ctl to find where this script is located, exit it and find the initial section with the apache2 directive in it and see if the variable that passes passes: OPTIONS , APACHE_ARGUMENTS , APACHE_DEFINES , or something else. Then see which file you need to edit by experimenting with /etc/sysconfig/httpd , /etc/default/apache2 or /etc/apache2/envvars .

+5
Nov 22 '09 at 20:08
source share

Here is a simple example that should be sufficient to change it to suit your requirements:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^localhost RewriteCond %{HTTP_HOST} !^www\. RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L] </IfModule> 
+1
Apr 10 '09 at 2:39
source share



All Articles