I would like to redirect users to my home page when the requested page or post cannot be found, but don't know how to go about with that.
How do I proceed with such and action?
I would like to redirect users to my home page when the requested page or post cannot be found, but don't know how to go about with that.
How do I proceed with such and action?
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.
You can set any page/url to handle a 404 error via the htaccess
file. This is a good tutorial on doing that: http://www.htaccessbasics.com/404-custom-error-page/ .
Use their example, but set the error page to a specific page (your home page).
Your theme may have a 404.php template that handles missing pages. But you can override that with the htaccess
file. Make sure you first make a backup of the existing htaccess
file, in case of problems.