Last month I showed you how you can redirect your old WordPress permalink structure to /%postname%/. Today I’d like to show you a much simpler snippet of code that helps redirect all 404 not found error messages to your home page.
This can be useful if you have a small website with very few pages. For example, say you have a small landing page website that you are using to collect leads for your email list. You may create dozens of landing pages. Rather than show a 404 error message to a visitor, you would convert higher if you just redirected the visitor to the home page.
To redirect all 404 not found errors to the home page, you all you have to do is add this to your .htaccess file:
ErrorDocument 404 /
The same code can be adapted to direct 404 errors to any page you want. For example,
ErrorDocument 404 /404-error-page.html
ErrorDocument 404 /404
If you’ve moved websites and haven’t redirected all url’s correctly, redirecting errors to your home page might be something you will consider. A good quality 404 that explains that the user has come across a page that is no longer available is much better though as it explains to the visitor why they aren’t seeing the page they thought they would.
If you are using WordPress, you may have a 404 solution built into your theme already. In WordPress, any theme that has a 404.php template will redirect all errors to www.website.com/404 automatically.
99% of the time you should be showing the 404 error page for errors however if you do need to redirect all errors to the home page, you may find the above code useful.