2

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?

andyb
  • 43,435
  • 12
  • 121
  • 150
user1187
  • 2,116
  • 8
  • 41
  • 74
  • Check this SO post [Sending a 404 error in PHP](http://stackoverflow.com/questions/437256/sending-a-404-error-in-php) – Nandakumar V Jan 25 '13 at 08:04
  • Are you manually navigating to `http://legascy.com/404_error/`, for example, are you typing that URL into a browser? If so that should (correctly) return a 200 OK since that page _does_ exist. – andyb Jan 25 '13 at 08:16

2 Answers2

1

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");
KoalaBear
  • 2,755
  • 2
  • 25
  • 29
0

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.

Salman
  • 9,299
  • 6
  • 40
  • 73