apache - 仅在访问特定域时使用 HTTP Auth

标签 apache authentication .htaccess

我有几个网站:example.com , example1.com , 和 example2.com .它们都指向我服务器的/public_html文件夹,这是我的 Apache 根文件夹。

我需要添加什么到我的.htaccess仅当用户来自 example2.com 时才使用 http 身份验证文件? example.comexample1.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 模块已启用(我使用 block 进行了验证),但似乎永远不会定义“testauth”,因为我的验证测试(重定向到 index2.php)没有执行(而它是在我的 block 中执行的)。任何想法为什么?

最佳答案

文档根目录中的 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.comdev.example2.com 等)。如果需要,可以调整表达式。任何其他主机都会导致 require_auth var 未设置,因此不需要身份验证。如果需要反过来,最后一行可以更改为:Allow from env=require_auth , 删除 ! .

关于apache - 仅在访问特定域时使用 HTTP Auth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1359472/

相关文章:

Apache 不在目录列表中显示 README.TXT 文件。我怎样才能启用它?

c# - 如何为 MVC .Net Core 应用创建单个管理员用户

apache - 使用 htaccess 重定向到移动版本,除非用户想要 "force desktop version"

Apache 2.4 mod_rewrite 和 htaccess

java - HttpClient 迁移 3.x 到 4.x

python - Django 和 apache 在不同的 docker 上

django - 如何使用 Apache、mod_wsgi 在 Django 项目中重新加载新更新?

c# - SignInManager,它是什么以及如何使用,何时使用?

javascript - 如何用 PHP 制作一个机器人来登录网站并在网站上做事

.htaccess - 在 DocumentRoot 后面的 Mod-Rewrite 加载文件