开发者

Domain name specific code blocks in htaccess

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 == "s开发者_开发知识库taging.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.


While doing it through the VirtualHost and not a .htaccess is much better, you can use the following solution if the setenvif module is active:

SetEnvIf Host ^dev\.site\.com$ is_on_dev_site
SetEnvIf Host ^staging\.site\.com$ is_on_dev_site
Order deny,allow
Deny from env=is_on_dev_site
# require password if access would otherwise be denied
Satisfy any
# Put your password auth stuff here

You could also do it with a whitelist which is probably better as it ensures your dev sites are still protected even if someone decides to allow access to them via www.dev.site.com etc.:

SetEnvIf Host ^site\.com$ is_on_public_site
SetEnvIf Host ^www\.site\.com$ is_on_public_site
Order deny,allow
Deny from all
Allow from env=is_on_public_site
Satisfy any
# Put your password auth stuff here

If you do not have mod_setenvif on your server, mod_rewrite can also do the work for you (replace the SetEnvIf blocks in the whitelist example with the following):

RewriteEngine On
RewriteCond %{HTTP_HOST} =site.com
RewriteRule ^ - [E=is_on_public_site:yes]
RewriteCond %{HTTP_HOST} =www.site.com
RewriteRule ^ - [E=is_on_public_site:yes]


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.


You should do this in your sites conf file with:

<VirtualHost domain.com:80>

...config statements here

</VirtualHost>

and

<VirtualHost domain2.com:80>

....config statements here

</VirtualHost>

If you are with a host who does not allow you to edit your sites config file, which is a real possibility if you are with a shared host then you should consider VPS or dedicated hosting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜