【问题标题】:Use HTTP Auth only if accessing a specific domain仅在访问特定域时使用 HTTP Auth
【发布时间】:2009-08-31 21:19:01
【问题描述】:

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

如果用户来自example2.com,我需要向我的.htaccess 文件添加什么才能使用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 模块已启用(我使用 块进行了验证),但似乎从未定义过“testauth”,因为我要验证的测试(重定向到 index2.php)是未执行(而它在我的 块中执行)。任何想法为什么?

【问题讨论】:

  • 如果所有域都指向同一个地方,为什么只保护其中一个?如果您实际上提供不同的内容,为什么不采用更明智的方法,例如每个域一个文件夹?
  • 我正在 /public_html 文件夹中运行多站点 Drupal 安装,除了该站点的正常用户登录之外,我还想在其中一个站点上创建一个全面的 http auth。
  • Drupal 的多站点允许我在同一组 PHP 文件中运行不同的网站,但使用不同的数据库(因此内容不同)。我可以在子文件夹中进行第二次安装,但这需要我维护不同的文件和模块集。
  • 根据文档httpd.apache.org/docs/2.2/mod/core.html#ifdefine,IfDefine 在启动时处理。
  • ifDefine 不适用于 setenv 或 setenvif 设置的内部环境变量,仅在服务器启动时使用 -D 标志

标签: apache authentication .htaccess


【解决方案1】:

文档根目录中的 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,删除 !

【讨论】:

  • 对我来说 !require_auth 不起作用,但这确实有效:Allow from all Deny from env=require_auth Require valid-user Satisfy any
  • 非常感谢
  • afaik 用户可以通过请求“example2.com”来利用此漏洞。 (注意最后的点)。所以你的规则应该是SetEnvIfNoCase Host example2\.com\.?$ ... 否则内容可能在没有保护的情况下仍然可见。
  • 我回滚了更改,@MaximKrizhanovsky 建议的方法在我的测试场景下不起作用,但原始答案可以。
【解决方案2】:

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>

【讨论】:

  • 有效。太棒了!
  • 我得到间隔服务器错误,建议? Apache 运行 2.4.37
  • @maestroosram 小心语法错误。你的 Apache 错误日志是怎么说的?
  • 像魅力一样工作!
【解决方案3】:

我想知道允许按 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 地址(即您和其他开发人员)。

【讨论】:

    【解决方案4】:

    这里有一个建议:

    创建一个名为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>
    

    【讨论】:

      【解决方案5】:

      您不应该将每个虚拟主机的配置放入 .htaccess。相反,将配置块放在 /etc/apache/sites-enabled/* 中正确配置文件的 VirtualHost 块中。

      【讨论】:

      • 很遗憾,我无权访问虚拟主机文件。我通过 CPanel 在共享托管服务器上运行所有这些。 =(
      猜你喜欢
      • 2018-12-13
      • 1970-01-01
      • 2014-08-13
      • 2012-11-17
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多