Correctly Return HTTP Status Code 404 for an Error Page

A 404 error page, also known as ErrorDocument 404, is primarily a page that informs the user that the requested resource does not exist.

For example, if a user follows a link whose target page no longer exists, the web server should display a so-called 404 error page.

However, a 404 error page is responsible for far more than this. When configured correctly, it informs the Googlebot about non-existent documents. To ensure that this happens the way it should, it is important that a 404 error page also returns the correct HTTP status code 404.

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

If the web server or the content management system (CMS) used is configured incorrectly so that an error page either returns the HTTP status code 200 (OK) or redirects users & the Googlebot to another page using a 301 redirect, this is referred to as a defective 404 page or a 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 differentiate between two use cases:

  • static 404 error page using the Apache web server and the .htaccess file
  • using the CMS WordPress and the existing 404.php file in the theme directory

.htaccess and Apache web server – 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 entering the following into the .htaccess file:

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

The screenshot shows how 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. You must first create the error page (e.g. 404.html) as a file.

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 create one if it does not exist (also in the root directory).
  • Enter “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.
  • Check if the correct HTTP status code 404 is returned here.
Test des HTTP-Statuscode 404 für eine URL

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

Setting up an error page in Nginx

If you use Nginx, you can set up a 404 error page by entering the following in the corresponding server block of the config file:

error_page 404/404.html

The only difference between these steps and those mentioned above is the entry in the config file and the absence of .htaccess. The rest of the workflow also applies here.

WordPress CMS – correctly configuring the error page

If you use the content management system (CMS) WordPress, setting the correct HTTP status code for the 404 error page is easy – provided the theme you use supports it.

Many WordPress themes, designs or templates have a corresponding 404.php file in the respective theme folder. Should you be unable to find this file, 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

Above: 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.
  • Check if the correct HTTP status code 404 is returned here.
Test des HTTP-Statuscode 404 für eine URL

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

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

A web server should return either the HTTP status code 404 (Not Found) or 410 (Gone) if a URL does not exist. If there are outdated links on the web or incorrect links on your website, Google can only recognise that the target page does not (or no longer) exists by an 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. Redirecting non-existent pages to the homepage through a 301 redirect is not recommended.

What exactly is a soft 404 error?

Webmasters who redirect content that no longer exists to other pages with a 301 redirect pose a problem for Google. An example:

If Google knows that the content of the page www.domain.com/recipe/how-to-make-red-gummy-bears/ is the best result for a search query for “How do I make red gummy bears?”, then Google will display this page at the top of the search results.

If the website operator now deletes this page but wants to keep the ranking for “How do I make red gummy bears?”, they can try to provide Google with a thematically similar page and point to it via a 301 redirect. In our example, this could be the category page for red gummy bears in the shop, www.domain.com/shop/red-gummy-bears/.

Google’s aim is to always provide the user with the best result for a specific search query.

However, if Google decides that the content of this page does not have the desired relevance for the search query, as was the case with the previous page, then Google can interpret this independently as a soft 404 error page.

The “soft” in this case simply describes that a page that technically does not return a 404 status code (page not found) is interpreted by Google in the same way as a page with an actual 404 status code.

What Google says

A good custom 404 page helps people find the information they're looking for, and also provides other helpful content that encourages people to explore your site further. (…) Custom 404 pages are created solely for users. Since these pages are useless from a search engine's perspective, make sure the server returns a 404 HTTP status code to prevent having the pages indexed.

Source: Search Console Help

Our Google conclusion

Pages that do not (or no longer) exist must also be clearly communicated to Google via a 404 status code.

Video Explanation by Matt Cutts / Google on this topic

Steve Paine