【问题标题】:cPanel Custom 401 Error Page "Not Displaying"cPanel 自定义 401 错误页面“未显示”
【发布时间】:2020-11-16 16:58:43
【问题描述】:

我的问题:

由于某种原因,我的自定义 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

谢谢!

【问题讨论】:

  • "3.关闭目录密码功能。" - 那么你是如何尝试触发 401 的呢?这应该允许您访问 401 ErrorDocument。
  • 我的意思是,我取消了 的“目录隐私”并尝试访问仍然受到保护的 。我认为受密码保护的 阻止了自定义错误页面的显示。不过没用。
  • 好的,应该可以的。确保您已清除浏览器缓存。
  • 我在不同的设备上使用过多个浏览器。

标签: .htaccess cpanel http-status-code-401 custom-error-pages errordocument


【解决方案1】:

要提供 401 ErrorDocument,它需要可访问。如果 一切 在 HTTP 身份验证之后被阻止,那么当身份验证失败并且您在默认服务器 401 响应中获得附加部分时,它将无法访问:

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

您需要在身份验证中“打一个洞”,并允许公众访问此文档。

例如:

<Files "error.php">
    Require all granted
</Files>
  1. 在不同的目录中创建了错误页面。

如果“不同的目录”也不受 HTTP 身份验证的保护,那应该可行。例如,子目录没有帮助。

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

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

如果您使用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

【讨论】:

  • 需要所有已授予的 似乎有效,但由于未包含 文件夹,因此页面上似乎没有加载任何内容.但是,我可以单击丢失图像中的超链接。您如何将多个文件添加到 标记以加载图像?
  • 是的,图像和任何其他外部资源(CSS、JS 等)自然也会被阻止。而且(如上所述)您仍然使用RewriteRule 指令阻止所有本地图像?如果您的错误文档使用了额外的资源,那么将所有这些都包含在它们自己的子目录中会更容易 - 然后您可以只允许对该子目录的公共访问,而无需识别每个文件。我会更新我的答案。
  • 我在 .htaccess 中添加了多个 标签来加载基本的 4 个文件。成功了,非常感谢 MrWhite!
  • 不客气。如果您需要允许访问的文件有 模式,则可以使用 &lt;FilesMatch&gt; 容器代替,它会为您提供正则表达式匹配。 &lt;Files&gt; 容器只有基本的 wildcard 匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-28
  • 2017-08-10
  • 2013-08-04
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
相关资源
最近更新 更多