cPanel 自定义 401 错误页"Not Displaying"



我的问题:

出于某种原因,我的自定义401错误页面在登录时没有显示,只是显示通用401错误页面:

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.

现在,我的自定义404错误页面和其他页面显示得很好,只是不适用于401。";error.php";(自定义错误页(文件在我的public_html目录中。

public_html目录中我当前的.htaccess文件:

ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 402 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
ErrorDocument 501 /error.php
ErrorDocument 502 /error.php
ErrorDocument 503 /error.php
ErrorDocument 504 /error.php
ErrorDocument 505 /error.php
ErrorDocument 506 /error.php
ErrorDocument 507 /error.php
ErrorDocument 510 /error.php
RewriteEngine On
DirectoryIndex .index.php .style.css .sorttable
RewriteRule .*.(jpg|jpeg|gif|png|bmp|zip|rar)$ - [F,NC]
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

#----------------------------------------------------------------cp:ppd
# Section managed by cPanel: Password Protected Directories     -cp:ppd
# - Do not edit this section of the htaccess file!              -cp:ppd
#----------------------------------------------------------------cp:ppd
AuthType Basic
AuthName "Protected 'public_html'"
AuthUserFile "/home/dark/.htpasswds/public_html/passwd"
Require valid-user
#----------------------------------------------------------------cp:ppd
# End section managed by cPanel: Password Protected Directories -cp:ppd
#----------------------------------------------------------------cp:ppd

现在我的目录是受密码保护的,你可以在.htaccess中看到。我尝试了以下修复,但在显示自定义401错误页面时仍然没有成功:

  1. 在其他目录中创建了错误页面
  2. 在.htaccess文件中指定了错误页面的完整url
  3. 已关闭目录密码功能
  4. 已将错误文件扩展名更改为.shtml
  5. 在stackoverflow网站上尝试了许多其他代码和问题

现在回顾一下,所有其他自定义错误页面都显示得很好,只是登录时没有401,我被难住了。

我的cPanel服务器信息:

Hosting Package Gold
Server Name server
cPanel Version  88.0 (build 14)
Apache Version  2.4.46
PHP Version 7.3.21
MySQL Version   10.3.24-MariaDB-cll-lve
Architecture    x86_64
Operating System    linux
Shared IP Address   45.95
Local IP Address    192.168
Path to Sendmail    /usr/sbin/sendmail
Path to Perl    /usr/bin/perl
Perl Version    5.16.3
Kernel Version  3.10.0-962.3.2.lve1.5.32.el7.x86_64

谢谢!

对于要提供的401 ErrorDocument,它需要是可访问的。如果所有都被阻止在HTTP身份验证之后,那么当身份验证失败时,它将无法访问,并且您在默认服务器401响应中获得额外的部分:

此外,在尝试使用处理请求的ErrorDocument。

您需要"打孔";在您的身份验证中,并允许公众访问此文档。

例如:

<Files "error.php">
Require all granted
</Files>
  1. 在其他目录中创建了错误页面

这本应该起作用;不同目录";也没有受到HTTP身份验证的保护。例如,子目录是没有帮助的。

  1. 在.htaccess文件中指定了错误页面的完整url

这将触发外部302重定向并丢失401响应。这是不允许的。如Apache文档中所述:

如果使用ErrorDocument 401指令,则它必须引用本地文档

  1. 关闭目录密码功能

如果您将";关闭目录密码功能";那么没有什么可以触发401,所以你的错误文档可以访问,但你是如何触发/测试你的401错误文档的呢?这看起来有点像鸡蛋。

  1. 将错误文件扩展名更改为.shtml

更改文件类型在这里没有什么区别。

RewriteRule .*.(jpg|jpeg|gif|png|bmp|zip|rar)$ - [F,NC]

为了澄清,您没有在任何页面上提供任何图像?任何包含这些文件扩展名之一的请求都将触发403 Forbidden。


更新:如果您的错误文档使用了额外的资源,那么这些文档将更容易被自己包含在自己的子目录中(例如/errordocs(,这样您就可以允许公众访问该子目录,而不必识别每个文件。

然后在/errordocs子目录中创建一个额外的.htaccess文件,其中只有一行:

Require all granted

最新更新