Redirect to home if page doesn’t exists [duplicate]

You can use this code inside your 404.php template file to safely redirect to users to homepage:

wp_safe_redirect(site_url());
exit();

Use this code before every line of code in your 404.php. This will redirect everyone who visits the 404 page to the website’s home URL, which would be what you are looking for.

You don’t have to delete the content of your 404.php file, since every line of code after the exit() will be ignored.