404 errors are the part of all websites and it usually comes when requested page not found on the web server. In this article, we will show you how to redirect 404 page to home page in WordPress.
To do this you don’t need to install the extra plugin on your site, A small PHP code will help you to do that.
Redirect 404 Error Pages to Homepage
All you have to do is go to your WordPress theme directory and open your 404.php file. If it doesn’t exist, then create a blank php file. Paste the following code in there:
<?php header("HTTP/1.1 301 Moved Permanently"); header("Location: ".get_bloginfo('url')); exit(); ?>
Done forget to replace .get_bloginfo(‘url’)with your website domain name to avoid extra request to WordPress like below.
<?php header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.example.com"); exit(); ?>
That’s all you have to do. Now when a user hits a 404 page, it will be redirected to the homepage.