404 page redirect to homepage to fix 404 errors in WordPress

Some times you can not edit your htaccess file to redirect all 404 error pages to homepage in WordPress, this is may be because you host your blog on a free server, or you have not a permission to edit this file, however, there is still solution to this issue, follow the following steps to make 301 redirect correctly:

  1. Login to your blog and go to the Dashboard.
  2. Go to Appearance menu and choose the item Editor.
  3. The page contains file name in the right side, find the file 404 Template (404.php) and click the link.
  4. You will see the code for the 404 page in your theme, at the head of the code and just after the tag <?php put the following lines.
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();

Note that you will need to clear cache if you have a cache plugin installed, after that, go to home page and try and page that is not in your site and check the result.

 

Comments
  1. No comments yet!, be the first to post a comment.

Your email will not be published, The required fields marked with '*'

*