我有几个网站:example.com
, example1.com
, 和 example2.com
.它们都指向我服务器的/public_html
文件夹,这是我的 Apache 根文件夹。
我需要添加什么到我的.htaccess
仅当用户来自 example2.com
时才使用 http 身份验证文件? example.com
和 example1.com
不应使用身份验证。
我知道我需要类似的东西
AuthType Basic
AuthName "Password Required"
AuthUserFile "/path/to/.htpasswd"
Require valid-user
但我只想在用户访问
example2.com
时要求输入密码.编辑
使用答案中建议的方法,我的 .htaccess 文件中有以下内容:
SetEnvIfNoCase Host ^(.*)$ testauth
<IfDefine testauth>
RewriteRule ^(.*)$ index2.php?q=$1 [L,QSA]
</IfDefine>
我知道 mod_setenvif.c 模块已启用(我使用
最佳答案
文档根目录中的 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
# Deny from everyone
Deny from all
# except if either of these are satisfied
Satisfy any
# 1. a valid authenticated user
Require valid-user
# or 2. the "require_auth" var is NOT set
Allow from env=!require_auth
这将使它不需要身份验证,除非主机以
example2.com
结尾。 (例如 www.example2.com
、 dev.example2.com
等)。如果需要,可以调整表达式。任何其他主机都会导致 require_auth
var 未设置,因此不需要身份验证。如果需要反过来,最后一行可以更改为:Allow from env=require_auth
, 删除 ! .
关于apache - 仅在访问特定域时使用 HTTP Auth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1359472/