HTTP 404 Not Found

The HTTP 404 status code indicates that the requested resource could not be found.

1521 views
d

By. Jacob

Edited: 2019-11-23 06:32

Servers deliver a HTTP 404 Not Found header response when a requested resource is not found, or when it has been deleted. The response will look like this:

HTTP/1.1 404 Not Found

The 404 response code is often sent with a message body in HTML containing a error message describing what went wrong to the user.

A good error page will both describe the problem, and show some alternative features or content to the user. This can be in the form of a search box or alternative content suggestions.

To deliver a 404 from PHP, we can use this code:

// Header response
header('HTTP/1.1 404 Not Found');

// A message to the browser
echo 'The resource was not found on this server.';

From PHP 5.4 and up you should use:

http_response_code(404);
echo 'The resource was not found on this server.'; // A message to the browser

Problems with 404 error pages

It's important that the correct header status code is delivered, along with the error page itself – as there is no reliable alternative method in the HTML. So-called soft 404 errors will not work.

A custom error page must be larger than 512 bytes to avoid problems with certain browsers and plugins. Otherwise, your page might be replaced with a default error build into the browser.

404 or 301 redirect to homepage

There is some debate in the SEO community as to whether you should redirect deleted pages to the homepage, or leave them deleted, which should produce a 404 error message.

Generally, it does not make sense to redirect deleted content, because users do not expect to be pushed silently around without any explanation as to what happened. It also has little or no affect on SEO, despite the rumors in the community. Reported negative effects are more likely to be other factors, such as having little or no content on the product pages of a webshop. For these reasons, I do NOT recommend you redirect deleted pages.

However, there are circumstances where you may want to show users to a different page, explaining that the content was deleted. Such a page could also show the user related or similar content to what they were requesting. Keep in mind, it would be semantically incorrect to use a 301 redirect for this purpose. Instead, you should correctly output a 404 response code.

Whether your CMS has this functionality already implemented is another matter.

Even if the page you are deleting has a lot of incoming links, you should still delete it. Outside factors, such as the number of incoming links, should not measure into your considerations. It should purely be a technical question relating to your CMS.

See also

  1. Creating a Custom Error Page – How to create a custom error page
  2. HTTP Response Codes – A list of HTTP Response Codes
  3. PHP Header – How to deliver status codes with PHP

Tell us what you think:

  1. An in-dept look at the use of headings (h1-h6) and sections in HTML pages.
  2. Pagination can be a confusing thing to get right both practically and programmatically. I have put a lot of thought into this subject, and here I am giving you a few of the ideas I have been working with.
  3. The best way to deal with a trailing question mark is probably just to make it a bad request, because it is a very odd thing to find in a request URL.
  4. How to optimize image-loading and automatically include width and height attributes on img elements with PHP.
  5. HTTP headers are not case-sensitive, so we are free to convert them to all-lowercase in our applications.

More in: Web development