Skip to main content
Login Join
Snippet · PHP

Redirect 404 to Homepage

Shared by Divyesh kakrecha · April 29, 2026 · @template_redirect

90 views
1 upvote
Back to Snippets

Automatically redirect 404 pages to the homepage.

/**
 * Redirect all 404 pages to the homepage.
 * WARNING: This creates soft 404s which can harm SEO.
 * Only use this if you have a specific reason to do so.
 */
add_action( 'template_redirect', function() {
    if ( is_404() ) {
        wp_safe_redirect( home_url() );
        exit();
    }
} );
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments