我们正在我们的网站上做一些主要工作,我们希望限制访问除维护页面以外的所有文件。我们希望所有用户被定向到该页面,如果取消或失败的授权请求。
ErrorDocument 401 /home/user/public_html/maintenance.html
ErrorDocument 403 /home/user/public_html/maintenance.html
<FilesMatch ^>
AuthName "Authorized Only"
AuthType Basic
AuthUserFile .htpasswd
require valid-user
</FilesMatch>
<Files "/home/user/public_html/maintenance.html">
Allow from all
</Files>这段代码似乎不起作用,用户被发送到一个页面,上面写着:
Unauthorized
This server could not verify that you are authorized to access the document requested.
Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't
understand how to supply the credentials required.
Additionally, a 401 Unauthorized error was encountered while trying to use an ErrorDocument
to handle the request.发布于 2020-10-29 03:55:18
您发布的代码存在许多问题:
<文件“/主目录/用户/公共html/maintenance.html”>
<Files>指令仅匹配文件基名,而不匹配整个文件系统路径。例如:只有maintenance.html。因此,上述方法永远不会成功。
ErrorDocument 401 /home/user/public_html/maintenance.html
ErrorDocument采用根相对URL路径,而不是绝对文件系统路径。例如:/maintenance.html。
AuthUserFile .htpasswd
但是,AuthUserFile指令的参数应该是绝对文件系统路径,而不是上面给出的相对路径。(相对路径在技术上是有效的,但它是相对于ServerRoot的,您可能没有权限将文件直接放到服务器根目录中!这是Apache配置中定义的ServerRoot,而不是服务器的根目录。)
解决方案
与使用单独的<Files>容器“允许”访问不同,您可以使用负向先行来排除该特定文件,使其不会触发密码提示。
例如:
ErrorDocument 401 /maintenance.html
<FilesMatch "^(?!maintenance\.html$).*">
AuthName "Authorized Only"
AuthType Basic
AuthUserFile /absolute/filesystem/path/to/.htpasswd
Require valid-user
</FilesMatch>https://stackoverflow.com/questions/64576247
复制相似问题