Redirect 404 page to WordPress home page.
404 is special kind of HTTP error which specify that the resource you are looking is not available.
Causes of 404 Error :
Many times we are not aware of the 404 errors occurred. There are many causes of the 404 error,
- When 3rd party sites link your URL or resource incorrectly
- You have made some URL structure change without permanent redirect so your old links are ended on 404 page.
- Sometimes you don’t want to keep some old posts then you may delete it. After deletion any request made to see that post results in 404 error.
Redirect 404 page to WordPress home page :
WordPress handles such type of common error more efficiently.Whenever resource is not available then by default user landed on 404 page provided by user.
Steps performed :
- Go to WordPress dashboard -> Appearance -> Editor
- Now in the theme editor find 404.php template.
- Add following code in the template and save.
1 2 3 4 5 |
<span style='color:#5f5035; background:#ffffe8; '><?php</span><span style='color:#000000; background:#ffffe8; '></span> <span style='color:#800000; background:#ffffe8; font-weight:bold; '>header</span><span style='color:#806030; background:#ffffe8; '>(</span><span style='color:#e60000; background:#ffffe8; '>"HTTP/1.1 301 Moved Permanently"</span><span style='color:#806030; background:#ffffe8; '>)</span><span style='color:#806030; background:#ffffe8; '>;</span><span style='color:#000000; background:#ffffe8; '></span> <span style='color:#800000; background:#ffffe8; font-weight:bold; '>header</span><span style='color:#806030; background:#ffffe8; '>(</span><span style='color:#e60000; background:#ffffe8; '>"Location: "</span><span style='color:#806030; background:#ffffe8; '>.</span><span style='color:#000000; background:#ffffe8; '>get_bloginfo</span><span style='color:#806030; background:#ffffe8; '>(</span><span style='color:#e60000; background:#ffffe8; '>'url'</span><span style='color:#806030; background:#ffffe8; '>)</span><span style='color:#806030; background:#ffffe8; '>)</span><span style='color:#806030; background:#ffffe8; '>;</span><span style='color:#000000; background:#ffffe8; '></span> <span style='color:#400000; background:#ffffe8; font-weight:bold; '>exit</span><span style='color:#806030; background:#ffffe8; '>(</span><span style='color:#806030; background:#ffffe8; '>)</span><span style='color:#806030; background:#ffffe8; '>;</span><span style='color:#000000; background:#ffffe8; '></span> <span style='color:#5f5035; background:#ffffe8; '>?></span> |
now when bad request is requested then page will automatically redirected to the home page.