我有几个网站: example.com
, example1.com
和example2.com
。 所有这些指向我的服务器的/public_html
文件夹,这是我的Apache根文件夹。
只有当用户来自example2.com
我需要添加到我的.htaccess
文件才能使用httpauthentication? example.com
和example1.com
不应该使用身份validation。
我知道我需要类似的东西
AuthType Basic AuthName "Password Required" AuthUserFile "/path/to/.htpasswd" Require valid-user
但是,如果用户访问example2.com
我只想要一个密码。
编辑
使用在答案中build议的方法,我的.htaccess文件中有以下内容:
SetEnvIfNoCase Host ^(.*)$ testauth <IfDefine testauth> RewriteRule ^(.*)$ index2.php?q=$1 [L,QSA] </IfDefine>
我知道mod_setenvif.c模块是启用的(我用<IfModule>块validation),但似乎“testauth”永远不会被定义,因为我的testingvalidation(redirect到index2.php)没有执行(而它正在执行我的<IfModule>块)。 任何想法为什么?
在文档根目录的htaccess文件中,如何处理这些内容:
# set the "require_auth" var if Host ends with "example2.com" SetEnvIfNoCase Host example2\.com$ require_auth=true # Auth stuff AuthUserFile /var/www/htpasswd AuthName "Password Protected" AuthType Basic # Setup a deny/allow Order Deny,Allow # Allow for everyone Allow from all # Except if a auth is required Deny from env=require_auth # But if you have a user you are welcome Require valid-user # Auth OR access, not AND https://wiki.apache.org/httpd/BypassAuthenticationOrAuthorizationRequirements Satisfy any
这将使得它不需要身份验证,除非主机以example2.com
结尾(例如www.example2.com
, dev.example2.com
等)。 表达式可以调整,如果需要的话。 任何其他主机将导致require_auth
var不被设置,因此不需要身份验证。 如果需要Allow from env=require_auth
,最后一行可以改为: Allow from env=require_auth
,删除! 。
以下是一个建议:
创建一个名为common.conf
的文件并保存在一个可访问的位置
在这个文件中,将所有站点(主机)共同使用的Apache配置。
删除当前的单个VirtualHost条目,替换为VirtualHost条目,如下所示:
# These are the password protected hosts <VirtualHost *:80> serverName example.com serverAlias example1.com Include /path-to-common-configuration/common.conf AuthType Basic AuthName "Password Required" AuthUserFile "/path/to/.htpasswd" Require valid-user </VirtualHost> # These are hosts not requiring authentication <VirtualHost *:80> serverName example2.com serverAlias example3.com Include /path-to-common-configuration/common.conf </VirtualHost>
您不应该将每个虚拟主机配置放入.htaccess。 相反,将配置块放在/etc/apache/sites-enabled/*
的适当配置文件中的VirtualHost块中。
我想知道是否可以通过允许访问每个IP地址的方法来帮助DanH?
就像是
SetEnvIf Remote_Addr 1\.2\.3\.4 AllowMeIn SetEnvIfNoCase Host this\.host\.is\.ok\.com AllowMeIn SetEnvIfNoCase Host this\.host\.is\.also\.ok\.com AllowMeIn
然后在你的Drupal“容器”
Order Allow,Deny Allow from env=AllowMeIn
应该做的伎俩。
任何“活”的主机都应该配置为“AllowMeIn”,否则你必须来自一个已知的IP地址(即你和其他开发者)。
Apache 2.4提供了一个If语句的语义选择:
<If "req('Host') == 'example2.com'"> AuthUserFile /path/to/htpasswd AuthType Basic AuthName "Password Protected" Require valid-user </If> <Else> Require all granted </Else>