When a visitor follows a broken or dead link, then he will see a 404 not found error page generated by your CMS or the server. This type of error page is generated when a search engine bot or a user is able to communicate with the server successfully, but the server is unable to find the requested content!

If you recently deleted lots of posts and pages from your WordPress powered blog, then you will start seeing lots of 404 errors in Google and Bing Webmaster tools. Let’s check out in this post how to get rid of these errors by 301 redirecting them to your homepage or some other URL?

Important Update: I will suggest you not to redirect lots of 404 pages to a single page, as it will result in soft 404 error. If there are lots of non-existing pages in your blog, then you should always return 404 or 410 Gone HTTP header for them OR redirect them to separate posts and pages of your blog.

If you redirect all non-existing pages to a single URL, then they will start returning 200 OK (success) code, which will result in soft 404 errors. These errors may also hurt your rankings in search results.

Redirect to Homepage

Follow these steps in order to redirect all 404 pages to your blog’s homepage:

1. Go to Appearance >> Editor in your dashboard and look for 404.php template file:

header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: “.get_bloginfo(‘url’));

2. Delete everything present in that file and enter following lines of code in it. Once done click on update file button.

Redirect to some other URL

Instead of homepage, if you’d like to redirect the 404 pages to some other page of your blog like sitemap etc. OR to some other external URL, then use following line of code in 404.php file:

header(“Status: 301 Moved Permanently”);
header(“Location: http://www.example.org/”);

In place of http://www.example.org/ above, you can use the URL of any other page of your blog (E.g. http://www.dailytechtuts.com/sitemap/) OR some external URL like https://www.google.co.in/.

Important notes:

  • Backup your current code of 404.php file before trying the above trick. There are many theme developers who bloats their code and editing or removing the content of your 404.php file may break your blog!
  • If there is no www in your blog’s web address, then there is no need to enter it.
  • The above lines of PHP code will 301 redirect user’s browser as well as bots of various search engines.
  • Your rankings will not be affected in any way, as we are using 301 permanent redirectionSee Update above.
  • The about trick will get rid of all existing 404 not found errors in Google and Bing webmaster tools, but in turn you will start seeing soft 404 errors. See Update above.