Skip to main content
Login Join
Snippet · PHP

Redirect 404 Pages to Homepage

Shared by Hiral solanki · September 9, 2026 · @template_redirect

1 copy
9 views
2 upvotes
Back to Snippets

For SEO, blindly redirecting every 404 to the homepage isn’t always recommended. A custom 404 page is often better.

/**
 * Redirect frontend 404 pages to the homepage.
 */
function mytheme_redirect_404_to_home() {

	if ( is_404() && ! is_admin() ) {
		wp_safe_redirect( home_url( '/' ), 301 );
		exit;
	}
}

add_action( 'template_redirect', 'mytheme_redirect_404_to_home' );
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