How To Redirect 404 Pages To Homepage in WordPress

404 pages, also known as “error pages,” are pages that are displayed by a web server when a user attempts to access a page that does not exist. This can happen for a number of reasons, such as if the user has mistyped the URL, if the page has been moved or deleted, or if the user is trying to access a page that is restricted or private.

When a 404 error occurs, the web server will typically display a default 404 error page that contains a message indicating that the requested page could not be found. This page will usually have a 404 status code in the HTTP headers, which indicates to the user’s browser that the page could not be found.

If you don’t want to show 404 pages in your WordPress website, you can redirect the visitors to your homepage instead of the 404 page.

Redirect 404 Pages to Homepage in WordPress

function redirect_404_to_homepage() {
  if (is_404()) {
    wp_redirect(home_url());
    exit;
  }
}
add_action('template_redirect', 'redirect_404_to_homepage');

This code defines a function called redirect_404_to_homepage that checks if the current page is a 404 error page. If it is, the function uses the wp_redirect function to redirect the user to the homepage. The add_action function is used to attach the redirect_404_to_homepage function to the template_redirect action, which allows the function to be executed when a page is loaded.

Keep in mind that this is just an example, and you may need to modify it to fit the specific needs of your WordPress site. Also, make sure to test the code thoroughly before using it on a live site, as redirecting 404 pages can have unintended consequences if not done carefully.

I hope this helps! Let me know if you have any questions.

In:

,