WordPress will typically redirect broken links and missing pages/posts to a 404 error page (404.php) that displays the appropriate error message for a page that could not be found. Sometimes instead of utilizing a 404 error page it’s better to redirect all 404 errors directly to your WordPress home page, this of course comes down to personal preference. In this tutorial we will teach you how to use PHP to redirect all 404 errors to the home page in WordPress.
Redirect all WordPress 404 Errors to the Home Page with PHP
Open your 404.php page if you already have one, if not then create a new PHP file and name it 404.php. Delete any and all code that is currently inside the 404.php page, then copy and paste the PHP redirect code below. After uploading the PHP file to your server it’s usually always a good idea to go ahead and refresh your permalinks. Open your site and test a broken link, all of your 404 errors should now be redirecting to your WordPress home page.
PHP<?php header("HTTP/1.1 301 Moved Permanently"); header("Location: ".get_bloginfo('url')); exit(); ?>
If you are having problems getting this tutorial to work please reread the tutorial and try again, if you still cannot get it to work please leave us a comment below and we will respond as soon as possible. Please do not email us with problems regarding this tutorial, only comments will be responded to.