配置 Apache 在 DocumentRoot 中而不是 ServerRoot 中查找配置文件 (.htpasswd)

问题描述 投票:0回答:1

我正在寻找一种方法来强制 Apache 查找相对于 DocumentRoot 而不是 ServerRoot 的文件。 现在,如果我按如下方式创建 .htaccess:

#Protect Directory
AuthName "Please provide valid username and password"
AuthType Basic
AuthUserFile .htpasswd
Require valid-user

Apache 在以下位置查找 .htpasswd:

/opt/homebrew/opt/httpd/.htpasswd

我想在以下位置查找它:

/opt/homebrew/var/www/FOLDER/.htpasswd

.htaccess 放置在:

/opt/homebrew/var/www/FOLDER/.htaccess

我不想更改 ServerRoot 并移动所有配置。

macos apache .htaccess homebrew .htpasswd
1个回答
0
投票

您无法改变这种行为。根据 AuthUserFile

 指令的 
Apache 文档,给予 AuthUserFile 指令的 文件路径
 必须是:

  • 绝对文件系统路径。例如:

    AuthUserFile /opt/homebrew/var/www/FOLDER/.htpasswd
    
    
或,

    相对于
  • ServerRoot的文件路径。
    
    
    但是,分配相对于文档根目录的路径也会违反
  • 安全建议
,该建议特别指出:

确保 AuthUserFile 存储在 Web 服务器的文档树之外。不要将其放在它保护的目录中。否则,客户端可能能够下载 AuthUserFile。

在您的示例(

AuthUserFile .htpasswd
)中,您似乎希望它相对于包含

.htaccess

 文件的目录,而不仅仅是文档根目录?
如果您需要在不同的服务器上提供不同的配置,那么您可以使用 

<If>
表达式(或其他条件)根据请求或服务器的元素有条件地设置

AuthUserFile

您还可以使用
已定义

变量。例如:

# In the server config Define DOCUMENT_ROOT /opt/homebrew/var/www

.htaccess

:
AuthUserFile ${DOCUMENT_ROOT}/FOLDER/.htpasswd


© www.soinside.com 2019 - 2024. All rights reserved.