404 page is part of every WordPress and non WordPress websites.
While working on WordPress you can write the designing code or 404 template file for your 404 page of the website. If you are in hurry to go live without 404-page design then there is a small coding trick that all 404’s page users will redirect to your Homepage.
In this very short article, we will provide you with the small piece of PHP code that redirect your 404 visitors to your website homepage.
It’s very simple, all you need to do is open your theme folder and 404.php file ( if not please create one ) and write the following code there and simply save it.
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url')); exit();
That’s all. when a user hits a 404 page will redirect to the homepage.
Note: This trick should be used if you are in hurry. But we recommend to track your 404 page and make proper design and redirect your user to proper 404 page.