Redirect all 404s to the homepage in WordPress

Redirect all 404s to the homepage in WordPress.

Snippet Type

Execute on Child Sites

Snippet

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

Just my 2 cents, it’s never a good idea to do this. See: SEO Anti-patterns: 301 redirect all your 404s to your homepage • Yoast and lots of others.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.