404 errors are a part of every website on the internet. When you code a single page website, you probably won’t have time to create a custom 404 page. In this case, you might want to redirect the 404 pages to your website’s homepage. Let’s discuss on how you can redirect the 404 pages to your homepage in WordPress.
Open your theme’s 404.php file which will be in the theme’s root folder. You can create a blank file if one doesn’t exist. Paste in the following code snippet:
<?php header("HTTP/1.1 301 Moved Permanently"); header("Location: ".get_bloginfo('url')); exit(); >
Now when a user hits a 404 page on your website, they will be redirected to the homepage. This should be sparingly used, we recommend you keep a track of your 404 pages and then redirect them appropriately.