How to correctly return the HTTP status code 404 for an error page

A 404 error page, also called an ErrorDocument 404, is, first and foremost, a page that informs the user that the requested resource does not exist. For example, if a user follows a link and the target page does not exist anymore, the webserver should show a 404 error page.

This is not the only job of a 404 error page, though. When configured correctly, it informs the Google-Bot if a document actually exists. To ensure this happens the way it should, it is important that a 404 error page returns the correct HTTP status code 404 – otherwise the 404 page is defective.

How do I return the correct HTTP status code for a 404 error page?

Webservers or the Content-Management-System (CMS) in use are often not set up correctly. This leads to the error page either returning the HTTP status code 200 (OK) or to a 301-redirect which sends the user as well as Google-Bot to another page. In both cases you would consider this to be a defective 404 page or a so-called soft 404 error.

In this article, we will discuss the correct configuration of a 404 error page with the appropriate HTTP status code 404. We will actually differentiate between two use-cases:

.htaccess and Apache webserver – correctly configuring the error page

Regardless of whether you use .html or .php files for your website or if you stick to a directory structure – a 404 Error Page is created by putting the following into the .htaccess-file:

The relative path to the error document is added to the .htaccess-file

The relative path to the error document is added to the .htaccess-file

Open or create the .htaccess-file and enter the relative path to the error page. First though, you have to create the error page (404.html, for example) on the first level of your website (the root-directory).

ErrorDocument 404 /404.html

The entire process, step by step:

  • create an error page (404.html or 404.php) on the first level of the website (root-directory)
  • open the .htaccess file – or crate one if you do not yet have one (also in the root-directory)
  • type in “ErrorDocument 404” followed by the relative path to the error page
  • save and request a page that does not exist, like http://www.your-domain.com/98899351
  • you should now see the contents of the error page 404.html
  • use httpstatus.io (for example) to check if the correct HTTP status code 404 is returned
Test des HTTP-Statuscode 404 für eine URL

The non-existent page https://www.sistrix.de/98899351 responds with the HTTP status code 404

WordPress CMS – correctly configuring the error page

If you use the Content-Management-System (CMS) WordPress, it is easy to return the correct error code for a 404 page – provided the theme you use supports it.

Many WordPress-themes, called designs or templates, use a 404.php-file in the theme-folder. Should you be unable to find the file, just set up an error page using .htaccess.

Open the 404.php file in your active WordPress theme and enter the following in the first line:

Configuring the HTTP status code 404 for a WordPress error page

Configuring the HTTP status code 404 for a WordPress error page

The error page 404.php can usually be found in the theme-directory in /wp-content/themes/active-themename/

<?php
header("HTTP/1.0 404 Not Found");
?>

The entire process, step by step:

  • find and open the error page in the WordPress theme folder (usually “404.php”)
  • add the above PHP source code to the first line and save the file
  • open a page that does not exist, like http://www.your-domain.com/98899351
  • you should now see the contents of the error page 404.html
  • use httpstatus.io (for example) to check if the correct HTTP status code 404 is returned
Test des HTTP-Statuscode 404 für eine URL

The non-existent page https://www.sistrix.de/98899351 responds with the HTTP status code 404

Why is it important for error pages to return the correct HTTP status code?

A webserver should return either the HTTP status code 404 (Not Found) or 410 (Gone) if a URL does not exist. In case that there are old links online or incorrect links on your website, Google will only be able to correctly realise that the target page does not exist (anymore) when you return the HTTP status code 404 or 410.

That is why you need to make sure that your website’s error page always responds with the correct HTTP status code 404. To redirect nonexistent pages to the landing page through a 301-redirect is not recommended, as it can cause problems.

Video Explanation by Matt Cutts / Google on this topic

How does Google handle “not found” pages that don’t return a 404?

How does Google deal with ‘page not found’ pages that are returning a 200 response code instead of a 404? Is this a form of spam? Can Google determine this mismatch algorithmically?

Additional information about this topic:

  • What are Soft-404 errors?

Related Topics

SEO Topics from A to Z