404 Header redirect query

18.5k views Asked by At

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?

6

There are 6 answers

0
Herbert On 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.

0
CodeCaster On

Simply said: yes, to both questions.

A 404-header sent from image.php won't redirect you to the 404 errordocument specified in .htaccess, so you'd have to manually redirect to 404.php whenever an image is not found. The preferred way is a 410 (Gone), especially since you know that the requested resource is removed and won't come back, so search engines can remove the link.

As for the second yes: it should, though Apache will already do that for you.

0
TimWolla On
  1. I don't know whether I would redirect, I'd rather include your 404.php
  2. That is correct. The default error-pages also exist and send a 404.
2
Gumbo On

The error documents defined using the ErrorDocument directive are only served when the web server recognizes that the request cannot be mapped onto an existing file. But as you’re using mod_rewrite to map them onto an existing image.php, they are obviously found. So from the web server perspective, the initial request can be fulfilled.

At this point, it doesn’t matter if the particular request cannot be fulfilled and results in a 404 later. In that case the image.php needs to respond with a proper error response.

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

You can only send one response status code. So you can either send an error response (4xx) or a redirection response (3xx). In your case, having an absolute URL as document URL will yield a redirect status code (i.e. 302) instead of the origin 404.

Should my 404 page actually send a 404 header?

Yes, of course. That’s what the status codes are meant for.

And do also provide some helpful information on that error documents. There is nothing worse than meaningless error documents.

1
the7oker On
header("HTTP/1.1 404 Not Found");
header("Refresh: 0; url=/404.php");
0
Dídac Rios On
header("HTTP/1.0 404 Not Found");

You may want to include the 404 page, sending only the header will give you a blank page

header("HTTP/1.0 404 Not Found");
include_once("404.html");
exit();