Alex Rivera | Logout

Domain name specific code blocks in htaccess

Asked 2010-11-01T12:47:48.030
19

We have local servers, central dev, staging and production servers. However the dev and staging are password protected for obvious reasons. So after deploying any changes to the htaccess i have to manually edit the htaccess file to enable the password protection on the dev and staging server.

Is there any way to have conditional blocks based on domain name like:

if ( $domain == "dev.example.com" || $domain == "staging.example.com" ){
  AuthName "Password Protected Area"
  AuthType Basic
  AuthUserFile /somewhere/.htpasswd
  Require valid-user
}

I need to find the htaccess equivalent of the condition:

if ( $domain == "dev.example.com" || $domain == "staging.example.com" ){

}

I would appreciate the any help or pointers you guys can give.

Edit
Report

1 Answer

5

I found a nice solution, to distinguish "localhost" vs. "live":

Since the conditionals of htaccess are somewhat limited why not settle for IfModule?: Compare the modules you have (i.e. using , and look for a significant, hopefully long-term difference, e.g. if you develop on windows and deploy on linux mod_win32.c might be good. ( Don't forget to add the .c which phpinfo() ommits.)

Then you can go about it like this (tested):

<IfModule mod_win32.c> 
    RewriteRule ^banana$ test.php?dudeThisIsLocal=1
</IfModule>
<IfModule !mod_win32.c> 
    RewriteRule ^banana$ test.php?dudeThisIsLive=1
</IfModule>

This example makes for a good sanity test, browing to yourdomain/banana resp. localhost/banana and if you (having rewrite enabled ) dump the $_GET array in test.php. If this works, fill the codeforks with your real config statements.

answered 2011-03-15T19:39:47.383

Your Answer