0

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?

5
  • If a page or post doesn't exists, i got the message: Oops! That page can’t be found. It looks like nothing was found at this location. Maybe try a search? Commented Jun 2, 2017 at 12:02
  • 1
    I wouldn't redirect users to a home page if the requested page can't be found. Rather, display a 404 error page. After that, you may redirect after a given amount of time, having informed the user that the requested page was not available, accounting for you redirecting him/her to the home page of your site. You may want to check this: Redirect Restricted Page to 404 Commented Jun 2, 2017 at 12:06
  • You may want to update your post as well with what you've tried by far as well as the challenge or error you are getting. Commented Jun 2, 2017 at 12:13
  • a little hint, use wp redirect on your 404.php theme
    – rheeantz
    Commented Jun 2, 2017 at 12:20
  • 1
    This is likely to be a frustrating experience for your users. Imagine trying to get to a page you think exists but doesn't, and you just keep landing on the home page. After a short period of frustration, they leave. 404 pages exist for a reason. A better strategy is to put a search box or a link to your home page on your 404 page. Commented Jun 2, 2017 at 23:30

2 Answers 2

1

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.

0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.