How to Redirect 404′s to your Home Page in WordPress

by Bradley on March 24, 2010

You’d be amazed how many 404 errors even the best websites have.  Short of having an amazing 404 page, the best quick fix is to redirect your 404 errors to your home page.

(And before the technical folks jump in and say all the reasons why a it’s a bad idea to redirect 404 errors to the home page, keep in mind that redirecting to the home page is a much better solution than the truly god-awful 404 page that comes standard with most WP themes.  And passing the link juice from links pointing to 404 pages to your home page is a huge benefit of following this method).

Update:  You can now download the 404.php file here and change the file name from 404.txt to 404.php and then just upload it to your theme folder.  It’s that easy.

Basically you want to have the code below in your 404.php in your theme folder.  Some themes don’t come with a 404.php file, so you’ll have to create one.  If you already have a 404.php file, then overwrite the contents of that file with this code:

//Simple Redirect for WordPress.
//Christopher Carey
//301 Redirect for WordPress
//http://wwww.noheat.com
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();

And you’ll need to add <?php to the front and ?> to the end so that the code runs.  I would have added it to the code snippet above but it keeps breaking.

Hope that helps and if you have any problems just drop some comments in the form below and I’ll help you out.

Want More out of WordPress?

I can show you how to get more traffic, make WordPress do amazing things, and how to make more money with your website by converting your visitors into customers. Click the button to the right to get a free estimate.

  • Fred

    Bradley, what about redirecting a 404 page to a specific page using thesis?
    It’s possiible?

    Thanks

  • http://pixelsheaven.com Ronny

    Thanks Bradley! This works like a charm.

  • http://www.shawnkcastille.com/blog Shawn K. Castille

    Great post Bradley! Keep up the great work, we need more knowledgeable bloggers like yourself.

  • http://frontiervillepardner.blogspot.com frontier

    thanks for the tip.. works perfectly..

  • http://www.lazymaths.com LazyMaths

    Thanks Bradley. This this works like a charm!

  • http://www.sellcent.com Matt Thomas

    works great thanks!!!

  • http://www.hoor.in Jacklyn Henkar

    the code you have given is for 301 redirect and not for 404 redirect

    • Bradley

      You are kind of right. It takes any page that has a 404 error and does a 301 redirect with the page.

      This is good for many reasons, but my favorite is that it should help your site keep linkjuice for links that head to a 404 page.

  • Tania

    Just what I needed – thanks!

  • http://MyDollarYourDream.com Piwacket

    I did this and it works great to redirect to my homepage, unfortunately my relatively linked images break (the offending “stub” is part of the base url… grrr)

    Help! Any advice appreciated.

  • http://www.mattdipasquale.com/ Matt Di Pasquale

    Thanks! Note: In PHP, it’s faster to use single quotes for text strings instead of double quotes when extrapolation of variables & special chars is unnecessary.

  • Talha

    Thank you. I was redirecting my 404′s to homepage manually each url by using a plugin, but now all my 404′s are redirected to homepage automatically.

  • http://woktoss.com woktoss

    Thank you Brad, i have a lot of dead links on Chinese search engines… this will fix ya!

  • http://www.vasathihousing.com/blog Vasathi

    Worked like a charm thanks !

  • http://www.funfavourite.com mukesh
  • http://web.overserv.net Michael

    I have put your solution into the 404.php, and have replaced ‘URL’ with /index.php, but it is saying that their is a problem with the redirecting??

    Am I doing something wrong?

    • Bradley

      What’s your website?

Previous post:

Next post: