问题描述
我们正在网站上进行一些重要工作,我们希望限制对除维护页面以外的所有文件的访问。如果取消或失败授权请求,我们希望将所有用户定向到该页面。
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.
解决方法
您发布的代码存在许多问题:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> <a href="javascript:void(0);" class="btn btn-primary d-inline mr-2" onclick="loadModalCopyGiftCardCode(12345667789988);">Show Code</a> <div class="modal fade" id="modalPopup" tabindex="-1" role="dialog" aria-labelledby="modalPopup" aria-hidden="true" data-backdrop="static"> <div class="modal-dialog modal-dialog-centered" role="document"> <div class="modal-content card payment"> <div class="modal-header"> <!-- Modal Title --> <div class="modal-title"> <!-- Custom Title--> <div id="modal-cus-title"></div> <!-- End Custom Title--> </div> <!-- End Modal Title --> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> </div> </div> </div> </div>
<Files "/home/user/public_html/maintenance.html">
伪指令仅匹配文件基名,而不匹配整个文件系统路径。例如。仅<Files>
。因此,以上将永远不会成功。
maintenance.html
ErrorDocument 401 /home/user/public_html/maintenance.html
采用相对于根的URL路径,而不是绝对文件系统路径。例如。 ErrorDocument
。
/maintenance.html
但是,AuthUserFile .htpasswd
伪指令的参数应该是绝对文件系统路径,而不是上面给出的 relative 路径。 (相对路径在技术上是有效的,但它相对于AuthUserFile
是相对的,您可能无权直接将文件放在服务器根目录中!这就是Apache配置中定义的ServerRoot
,而不是服务器的根目录。)
解决方案
除了使用单独的ServerRoot
容器来“允许”访问外,您还可以使用否定先行排除该特定文件来触发密码提示。
例如:
<Files>