404错误页面显示HTTP状态200而不是404

时间:2013-01-25 07:58:56

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

我有以下用于执行重定向的代码。如果我的$includeFile不存在,则会重定向到404页面。这是我index.php

的摘录
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。

如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您需要在“404_error”页面上放置404的标题功能,而不是重定向的页面。这是因为浏览器被发送到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");

答案 1 :(得分:0)

您之前发送404错误代码的标题会被Location标头丢弃,浏览器会将用户带到新页面。您重定向到的页面可用,因此它将始终显示200。

相关问题