How to redirect 404 pages in WordPress to homepage without plugin?

When visitors land on a 404 Not Found page, it means the URL doesn’t exist — maybe the post was deleted, renamed, or the link was wrong.
Instead of showing a dead 404 page, you can automatically redirect users to your homepage to improve user experience and SEO.

Let’s do it without using any plugin — clean and lightweight.

Step 1 : Create a child theme

If you have not created a child theme yet then first create a child theme

Visit to know : How to create child theme in WordPress

If you have already created a child theme then ignore step 1.

Step 2 : Copy the following code and paste it into your active theme’s functions.php file or your child theme (recommended):

// Redirect all 404 pages to homepage
function redirect_404_to_homepage() {
    if (is_404()) {
        wp_redirect(home_url());
        exit;
    }
}
add_action('template_redirect', 'redirect_404_to_homepage');

What this does:

  • Checks if the requested page is a 404
  • Instantly redirects the visitor to your homepage
  • Prevents users from seeing broken links

If you prefer to send users to a custom “Not Found” page instead of the homepage, use this version:

function redirect_404_to_custom_page() {
    if (is_404()) {
        wp_redirect(home_url('/custom-page/'));
        exit;
    }
}
add_action('template_redirect', 'redirect_404_to_custom_page');

Replace /custom-page/ with your desired page slug (for example, /help/ or /blog/).

Step 4: How to Test Your Redirect ?

Just visit couple of random URLs

yoursite.com/test404page
yoursite.com/randomurl

You should be redirected to your homepage immediately.

If it doesn’t work, clear your site and browser cache and retry it will work.

Why Redirect 404 Pages

  • Improves user experience (keeps visitors on your site)
  • Reduces bounce rate (helps SEO indirectly)
  • Prevents showing blank error pages
  • Good for sites with many deleted or outdated posts

Leave a Reply

Your email address will not be published. Required fields are marked *