简体   繁体   English

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

[英]404 error page showing HTTP Status 200 instead of 404

I have the following code for performing redirection. 我有以下用于执行重定向的代码。 If my $includeFile does not exist it will redirect to a 404 page. 如果我的$includeFile不存在,它将重定向到404页面。 This is a snippet from my index.php 这是我的index.php的片段

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

But http://legascy.com/404_error/ is showing HTTP Status 200 OK instead of 404. 但是http://legascy.com/404_error/显示的是HTTP状态200,而不是404。

How to solve this? 如何解决呢?

You need to place the header function for 404 on the "404_error" page instead of the page that redirects. 您需要将404的标头函数放在“ 404_error”页面上,而不是重定向页面上。 This because the browser is sent to 404_error which has the default 200 status code. 这是因为浏览器被发送到404_error,它具有默认的200状态代码。

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

And on 404_error/index.php: 并在404_error / index.php上:

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

Your previous header for sending 404 error code is discarded by the Location header and the browser takes the user to new page. 您之前发送404错误代码的标头已被Location标头丢弃,浏览器将用户带到新页面。 The page you redirected to is available, so it will always show 200. 重定向到的页面可用,因此它将始终显示200。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM