Php 404错误页面显示HTTP状态200而不是404
我有以下代码来执行重定向。如果我的Php 404错误页面显示HTTP状态200而不是404,php,redirect,seo,http-headers,http-status-code-404,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
$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
如何解决这个问题?您需要将404的头函数放在“404\u错误”页面上,而不是重定向页面上。这是因为浏览器被发送到404_错误,该错误具有默认的200状态代码
if ( !file_exists($includeFile) ) {
header("Location: http://legascy.com/404_error/");
}
在404_error/index.php上:
header("HTTP/1.1 404 Page Not Found");
“位置”标题会丢弃您以前发送404错误代码的标题,浏览器会将用户带到新页面。您重定向到的页面可用,因此它将始终显示200。选中此项,以便手动导航到
http://legascy.com/404_error/
,例如,您是否在浏览器中键入该URL?如果是这样,那么应该(正确地)返回200 OK,因为该页面确实存在。