Redirect all HTTP error 404 to your main page in WordPress

What happens if a user accesses a page or a post on your WordPress site that no longer exists?  Do they simply get directed to an ugly page that says “Not found?”  This may deter users from remaining on your site, and can also work against you for your pagerank.  Wouldn’t it be better if the user saw your homepage instead, which would include a search bar (for most) and a nice, new list of posts?  How would you do that?

Most themes come with a 404.php file that WordPress calls in the event of a not found error.  This can be found in you WordPress installation directory, under wp-content/themes/{theme folder}/404.php.  If there is already a file there, you can over write it, and if not, create a new one and insert the following code:

<?php
header("HTTP/1.0 404 Not Found");
header("Location: ".get_bloginfo('url'));
exit();
?>

Ensure that this is the only code in the file.  This will redirect the user back to the url that is configured in Settings, and no more ugly 404 page.  It will also properly inform search engines that the specified URL does not exist. I would also recommend keeping a backup of this file, perhaps in the base themes folder, just in case you change or upgrade your them and the 404.php file gets overwritten.

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>