【问题标题】:404 error page showing HTTP Status 200 instead of 404404 错误页面显示 HTTP 状态 200 而不是 404
【发布时间】:2013-01-09 04:06:48
【问题描述】:

我有以下代码用于执行重定向。如果我的$includeFile 不存在,它将重定向到 404 页面。这是我的index.phpsn-p

if ( !file_exists($includeFile) )
     Header( "HTTP/1.1 404 Page Not Found" );
     Header( "Location: http://legascy.com/404_error/");

但http://legascy.com/404_error/ 显示 HTTP 状态 200 OK 而不是 404。

如何解决?

【问题讨论】:

  • 查看此 SO 帖子 Sending a 404 error in PHP
  • 您是否手动导航到http://legascy.com/404_error/,例如,您是否在浏览器中输入该 URL?如果是这样,那应该(正确地)返回 200 OK,因为该页面 确实 存在。

标签: php redirect seo http-headers http-status-code-404


【解决方案1】:

您需要将 404 的标头函数放在“404_error”页面上,而不是重定向的页面上。这是因为浏览器被发送到 404_error,它有默认的 200 状态码。

if ( !file_exists($includeFile) ) {
     header("Location: http://legascy.com/404_error/");
}

在 404_error/index.php 上:

header("HTTP/1.1 404 Page Not Found");

【讨论】:

    【解决方案2】:

    您之前发送 404 错误代码的标头被 Location 标头丢弃,浏览器会将用户带到新页面。你重定向到的页面是可用的,所以它总是显示 200。

    【讨论】:

      猜你喜欢
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      相关资源
      最近更新 更多