user1187
user1187

Reputation: 2198

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. This is a snippet from my 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.

How to solve this?

Upvotes: 2

Views: 3640

Answers (2)

Salman
Salman

Reputation: 9447

Your previous header for sending 404 error code is discarded by the Location header and the browser takes the user to new page. The page you redirected to is available, so it will always show 200.

Upvotes: 0

KoalaBear
KoalaBear

Reputation: 2948

You need to place the header function for 404 on the "404_error" page instead of the page that redirects. This because the browser is sent to 404_error which has the default 200 status code.

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

And on 404_error/index.php:

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

Upvotes: 1

Related Questions