March 24, 2010 - 30 comments

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

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.

Comments

Fred
April 3, 2010 at 11:24 pm

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

Thanks

Ronny
April 9, 2010 at 3:25 am

Thanks Bradley! This works like a charm.

Shawn K. Castille
July 6, 2010 at 4:46 pm

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

frontier
July 23, 2010 at 4:11 am

thanks for the tip.. works perfectly..

LazyMaths
August 5, 2010 at 12:05 pm

Thanks Bradley. This this works like a charm!

Matt Thomas
August 16, 2010 at 11:38 pm

works great thanks!!!

Jacklyn Henkar
August 30, 2010 at 1:33 am

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

    Bradley
    August 30, 2010 at 9:47 am

    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
September 2, 2010 at 2:18 am

Just what I needed – thanks!

Piwacket
October 30, 2010 at 5:52 pm

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.

Matt Di Pasquale
December 22, 2010 at 10:08 am

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
January 4, 2011 at 2:36 am

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.

woktoss
April 28, 2011 at 3:44 am

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

Vasathi
May 28, 2011 at 12:44 am

Worked like a charm thanks !

mukesh
June 27, 2011 at 9:09 pm

Michael
August 16, 2011 at 8:57 am

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
    August 17, 2011 at 6:20 am

    What’s your website?

John
February 25, 2012 at 11:59 am

doesnt work for me

    Anonymous
    February 25, 2012 at 1:06 pm

    John,

    Will you post some more details and we’ll see if we can get it to work for ya?

    Best, Brad

Itubaru
February 27, 2012 at 8:41 pm

well, i use the code and it work fine on my site (htxp:// itubaru com)

Amir Gbg
March 7, 2012 at 5:26 am

hello I had installed WP in a file called “SV”. Recently I reinstalled WP but this time in the root file. But anyone who has put the page in their favorites clicked the old address and got error. Now it works just fine. Thanks again for this shared idea

fembat
May 14, 2012 at 12:25 am

Worked like a charm. Thank you!

    rawwebdesign
    May 14, 2012 at 6:31 am

    approved

Konrad Braun
June 8, 2012 at 8:51 am

Just what I was looking for. I am no techie so this was perfect – spent over an hour trying to figure things out before I got this. You saved my day :) 

Thank you!

George918
October 24, 2012 at 2:40 pm

thanks worked great

Adam Klein
January 7, 2013 at 3:03 am

this dont work for me…

    rawwebdesign
    January 7, 2013 at 5:51 am

    Can you tell me more about how you implemented it and maybe we can find the problem?

SLee and Topher
March 18, 2013 at 2:31 pm

Thanks, Bradley. This is just what I needed.

Lindsay Cohen
March 21, 2013 at 10:44 am

Hi Bradley, thanks for the tutorial. How would I redirect the 404 to another page on my site, besides the front page without mesing up the code on your 404.txt?

Abu Alhaytham
April 5, 2013 at 8:38 pm

thank you ,really simple and useful code

Leave a Reply