How to Set a 410 ‘Gone’ Status Code for 404 Pages in WordPress
How to Set a 410 ‘Gone’ Status Code for 404 Pages in WordPress – Changing the HTTP status code from 404 (Not Found) to 410 (Gone) on your WordPress site can be useful for informing search engines and users that a resource is not just missing, but permanently removed.
This can potentially aid in SEO by more quickly removing deprecated URLs from search engine indices.
Understanding HTTP Status Codes 404 and 410
Like any other HTTP Status Codes, 404 and 410 have different meaning:
- 404 Not Found: This status code indicates that the requested resource could not be found but may be available in the future. It’s a common response for missing pages and doesn’t necessarily imply that the resource has been permanently removed.
- 410 Gone: This status code is used when the resource was available in the past but has been permanently removed and will not be available again. It’s a stronger signal than 404 for search engines to remove the URL from their index.
Implementing the Status Code Change
To change the status code from 404 to 410 for pages that are not found, you can use the custom code you provided. This code should be placed in a strategic location within your WordPress installation.
Here are the options for implementing this:
Option 1: Using the Theme’s functions.php
File
Adding the provided code to your theme’s functions.php
file is a straightforward approach. However, for long-term changes and to avoid issues during theme updates, it’s better to use a child theme. Here’s how you can do it:
- Navigate to your child theme’s directory.
- Open the
functions.php
file. - Add the provided PHP code at the end of the file.
function zerobytecode_set_410_for_404_pages() {
if (is_404()) {
status_header(410);
nocache_headers();
}
}
add_action('template_redirect', 'zerobytecode_set_410_for_404_pages');
By using a child theme, you ensure that your modifications are preserved even when the parent theme is updated.
Option 2: Using a Site-Specific Plugin for Custom Code Snippets
For a more modular and update-proof method, consider using a site-specific plugin or a plugin like “Code Snippets” to manage this modification:
- Install the “Code Snippets” plugin from the WordPress plugin repository.
- Activate the plugin.
- Go to Snippets > Add New.
- Paste the provided code into the code area.
- Name your snippet (e.g., “Set 410 for 404 Pages“) and activate it.
This approach keeps your code separate from theme files and allows easy management and deactivation of the code snippet if needed.
Wrapping Up
Setting a 410 status code for pages that would normally return a 404 can help streamline your site’s interaction with search engines and users by clearly communicating the permanent removal of content.
Whether you choose to add the code directly to your child theme or use a plugin, this modification is a powerful way to manage your site’s HTTP responses effectively.