首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >适用于401和403的.htaccess AuthType Basic FilesMatch,但maintenance.html除外

适用于401和403的.htaccess AuthType Basic FilesMatch,但maintenance.html除外
EN

Stack Overflow用户
提问于 2020-10-28 23:37:36
回答 1查看 42关注 0票数 2

我们正在我们的网站上做一些主要工作,我们希望限制访问除维护页面以外的所有文件。我们希望所有用户被定向到该页面,如果取消或失败的授权请求。

代码语言:javascript
复制
    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>

这段代码似乎不起作用,用户被发送到一个页面,上面写着:

代码语言:javascript
复制
    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.
EN

回答 1

Stack Overflow用户

发布于 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>容器“允许”访问不同,您可以使用负向先行来排除该特定文件,使其不会触发密码提示。

例如:

代码语言:javascript
复制
ErrorDocument 401 /maintenance.html

<FilesMatch "^(?!maintenance\.html$).*">
    AuthName "Authorized Only"
    AuthType Basic
    AuthUserFile /absolute/filesystem/path/to/.htpasswd
    Require valid-user
</FilesMatch>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64576247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档