【问题标题】:How to send correct HTTP response on custom Error document?如何在自定义错误文档上发送正确的 HTTP 响应?
【发布时间】:2013-09-22 03:18:53
【问题描述】:

我通过.htaccess 配置了我的网站来执行此操作:

ErrorDocument 403 http://www.mydomain.com/error.php?e=403
ErrorDocument 401 http://www.mydomain.com/error.php?e=401
ErrorDocument 400 http://www.mydomain.com/error.php?e=400
ErrorDocument 500 http://www.mydomain.com/error.php?e=500

error.php 记录错误并显示用户/客户友好的错误页面。

另外,我添加了这段代码:

if($_GET['e'] == '404' || $_GET['e'] == '403' || $_GET['e'] == '500' )
{   $error_no = $_GET['e']; } else { $error_no = '200'; }
header(' ', true, $error_code);

所以当我访问mydomain.com/pagedoesnotexist 时,我看到错误页面(但地址栏仍然显示不存在的 URL),我收到一个报告说 404 页面被触发,但我查看了我的服务器日志文件,这就是它所说的: MY.IP.000.00 - - [date and time] "GET /pagedoesnotexistHTTP/1.1" 200 2450 "-" "USERAGENTINFO"

在我的服务器日志中,我看不到任何关于 404 的信息……只有 200……这是为什么呢?我怎样才能让它发回 404 代码? 现在我假设机器人等总是得到 200,这意味着访问不存在的网站时可以得到响应......或者我错过了什么?

*题外话:2450 代表什么? O_o*

【问题讨论】:

    标签: php .htaccess http-headers httpresponse errordocument


    【解决方案1】:

    但是请注意,您错误地设置了ErrorDocument,这会导致外部重定向并返回200,而不是404403 等。将ErrorDocument 设置如下没有域名强>:

    ErrorDocument 403 /error.php?e=403
    ErrorDocument 401 /error.php?e=401
    ErrorDocument 400 /error.php?e=400
    ErrorDocument 500 /error.php?e=500
    

    这将在您的浏览器中保留原始未找到的 URL,同时返回正确的 HTTP 状态代码。

    关于您的代码:

    您正在设置变量 $error_no 并在 header() 函数中使用 $error_code。你应该使用:

    if($_GET['e'] == '404' || $_GET['e'] == '403' || $_GET['e'] == '500' )
    {   $error_no = $_GET['e']; } else { $error_no = '200'; }
    header(' ', true, $error_no);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-07
      • 2020-01-14
      • 1970-01-01
      • 2016-04-29
      • 2020-03-17
      • 2019-05-01
      • 1970-01-01
      相关资源
      最近更新 更多