Php – 404 Header redirect query

http-headershttp-status-code-404http-status-code-410PHPseo

On a standard LAMP application i am sending people to my 404 page using a .htaccess rule like so:

ErrorDocument 404 http://www.mydomain.com/404.php

We serve dynamic images using a php file which reads files from the filesystem, i've just noticed though that when an image is deleted from the app that we're not picking this up so a request for http://www.mydomain.com/image_4.jg (for example) which calls image.php using mod_rewrite doesn't redirect the user to the 404 page as the dynamic image file will always exist. In this case i know i should be using a 404 but i'm not sure where.

Obviously i know i need to manually insert a header redirect to the 404.php page when the image has been deleted but should i actually send a 404 header with this redirect?. Looking at the code our 404.php page actually sends a 404 header already with ("HTTP/1.1 404 Not Found"); which is what our SEO team had instructed a few years back, not sure if this is correct or not?. It seems counter-intuitive to me as it would almost imply that the 404 page itself has not been found.

I guess this is 2 questions then:

  • Should i send a 404 header within the redirect when the image is not
    found?
  • Should my 404 page actually send a 404 header?

EDIT

It doesn't actually seem possible to send a 404 and redirect at the same time, for example this causes Chrome to show a "Oops! This link appears to be broken." message

header( "Location: /404.php", true, 404 );

If you break up the headers like this it also doesn't work as intended

header("HTTP/1.1 404 Not Found");
header("Location: /404.php" );

In this case, if you look at the headers it sends a 302 followed by a 404. Is it enough in this case to maybe just send a header without a redirect? maybe just send a 410 as recommended by some?

Best Answer

404 Not Found

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.

Status Code Definitions

A 404 response should be sent back to the client when a resource is not found.

Should i send a 404 header within the redirect when the image is not found?

Yes. This tells the client requesting the dynamic image that the resource wasn't found. If you know the resource will never return, use 410 (Gone) instead.

Should my 404 page actually send a 404 header?

Yes. See 404 Error Pages and Redirects for SEOs

Update

header("HTTP/1.0 404 Not Found"); should be sufficient; however, Google Chrome fails to display a custom 404 page if the content is less than 512 bytes in length.