Redirect 404 Pages to the Homepage
Sends visitors who hit a missing page to the homepage instead of showing the 404 template.
Opens WPIDE on
Code
add_action( 'template_redirect', function () {
if ( is_404() ) {
wp_safe_redirect( home_url( '/' ), 301 );
exit;
}
}, 99 );
Notes
Search engines treat mass redirects to the homepage as soft 404s. A helpful 404 page or specific redirects are usually better.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.