Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess AuthType基本文件匹配401和403的除maintenance.html之外的所有文件_.htaccess_Http Status Code 401_.htpasswd - Fatal编程技术网

.htaccess AuthType基本文件匹配401和403的除maintenance.html之外的所有文件

.htaccess AuthType基本文件匹配401和403的除maintenance.html之外的所有文件,.htaccess,http-status-code-401,.htpasswd,.htaccess,Http Status Code 401,.htpasswd,我们正在我们的网站上做一些主要的工作,我们想限制访问除维护页面以外的所有文件。我们希望,如果用户取消或未通过授权请求,所有用户都将被定向到该页面 ErrorDocument 401 /home/user/public_html/maintenance.html ErrorDocument 403 /home/user/public_html/maintenance.html <FilesMatch ^> AuthName "Authorize

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

    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>

您发布的代码存在许多问题:

ErrorDocument
采用根相对URL路径,而不是绝对文件系统路径。例如,
/maintenance.html

但是,
AuthUserFile
指令的参数应该是绝对文件系统路径,而不是上面给出的相对路径。(相对路径在技术上是有效的,但它是相对于
ServerRoot
的,您可能无权将文件直接放入服务器根目录中!这是Apache配置中定义的
ServerRoot
,而不是服务器的根目录。)


解决方案 与使用单独的
容器来“允许”访问不同,您可以使用负前瞻来排除触发密码提示的特定文件

例如:

ErrorDocument 401 /maintenance.html

<FilesMatch "^(?!maintenance\.html$).*">
    AuthName "Authorized Only"
    AuthType Basic
    AuthUserFile /absolute/filesystem/path/to/.htpasswd
    Require valid-user
</FilesMatch>
ErrorDocument 401/maintenance.html
AuthName“仅授权”
AuthType Basic
AuthUserFile/absolute/filesystem/path/to/.htpasswd
需要有效用户
<Files "/home/user/public_html/maintenance.html">
ErrorDocument 401 /home/user/public_html/maintenance.html
AuthUserFile .htpasswd
ErrorDocument 401 /maintenance.html

<FilesMatch "^(?!maintenance\.html$).*">
    AuthName "Authorized Only"
    AuthType Basic
    AuthUserFile /absolute/filesystem/path/to/.htpasswd
    Require valid-user
</FilesMatch>