我正在寻找一种方法来强制 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 并移动所有配置。
您无法改变这种行为。根据 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