htaccess的域名具体code块 [英] Domain name specific code blocks in htaccess

查看:209
本文介绍了htaccess的域名具体code块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有本地服务器,集中开发,分期和生产服务器。然而,开发和分期有密码保护的原因是显而易见的。所以,在部署任何更改htaccess的后,我必须手动编辑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 == "staging.example.com" ){
  AuthName "Password Protected Area"
  AuthType Basic
  AuthUserFile /somewhere/.htpasswd
  Require valid-user
}

我需要找到htaccess的等价条件的:

I need to find the htaccess equivalent of the condition:

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

}

我会AP preciate的任何帮助或指针你们可以给。

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

推荐答案

在做的过程通过虚拟主机,而不是一个htaccess的要好得多,你可以使用下面的解决方案,如果SetEnvIf之后模块被激活:

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

您也可以做到这一点与白名单这可能是更好的,因为它可以确保你的开发站点仍然受到保护,即使有人决定允许访问他们通过www.dev.site.com等:

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

如果您还没有mod_setenvif您的服务器上,mod_rewrite的还可以做的工作适合你(在白名单中的例子与替换SetEnvIf之后块以下):

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]

这篇关于htaccess的域名具体code块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆