Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Redirect your 404 page to the Home Page in WordPress

Last updated on by
Elegant Themes
How to Redirect your 404 page to the Home Page in WordPress

404′s are part of every website. A while ago we compiled a list of some pretty cool WordPress 404 designs. When coding a one page site, you might not have the time to create a custom 404 page. In which case, you might as well redirect the 404 page to your site’s homepage. In this article, we will show you how to redirect 404 page to home page in WordPress.

All you have to do is open your 404.php file in your theme’s folder. If it doesn’t exist, then create a blank php file. Paste the following code in there:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();
?>

That’s all. Now when a user hits a 404 page will be redirected to the homepage.

Note: This should be used in very specific cases. In most other cases (i.e blogs, portfolios etc), you should track your 404 pages and redirect them appropriately.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Roy says:

    I am very grateful for this. You saved me doing 200+ redirects. Worked a treat.

  2. P.D. says:

    Does anyone know if the code works as well for multisite installations?

    If so, does the 404 page bounce to the subdomain / subfolder / subsite or the main or root domain / folder / site?

  3. Vishal says:

    Thanks dude…. :)

  4. Prasanna says:

    I really thank full to you. My website here after won’t get crawl errors.

    Thanks a lot.

    :)

  5. marco says:

    Thanks that help a lot.

  6. Dilpreet Bhatia Music says:

    Wow – That Was easy!!

  7. Filip Hajek says:

    There is a cool WP plugin “404 Redirected”, whoch captures 404s for you and let’s you redirect them easily.

    • Eric says:

      Thanks for the comment! That helped me out a lot! The article’s has a good solution too but I don’t like messing with my code. Thanks again for the suggestion Flip!

      -Eric Out-

  8. Nikole says:

    What if I want t redirect to a specific page not the homepage?

    • Editorial Staff says:

      You can change line 3. Or better use the redirection plugin like we mentioned in the article.

  9. Rakesh says:

    It rocks!!! I used the code ( previously my theme given me a custom 404 page template, but I replaced with your few lines). Thank you.

  10. ud says:

    Thanks for your very useful information

  11. Arslanh says:

    I did this but it doesn’t seem to work. I have no knowledge of coding so please help me!

    • Editorial Staff says:

      It should work just fine if you follow the article how it is. This is not a very complicated piece of code.

  12. Julien says:

    I am really not convinced by the relevance of this. 404 means the page doesn’t exist anymore, 301 is a permanent redirect. Those 2 errors do not mean the same for search engines.

    • Rootpak says:

      I agree. I would not recommend redirecting all 404 pages to the home page. From SEO point of view it is not accepyable. It means that every not existing page was moved to home page. It can simply lead to problems how google sees your site.

  13. Andy Feliciotti says:

    I use smart 404 extension and I must say it’s amazing, it’ll find a post with a similar URL and redirect to it

  14. Clark Wimberly says:

    I should know better, but one time I created an infinite loop using something close to this. It was a site with few pages for mostly logged-in users, so I made my 404 template redirect to the user page. What I had forgotten is that I made my user page redirect to the 404 if you weren’t signed in. Totally my fault, and barely relevant, but there you go.

    • Editorial Staff says:

      Hah that’s funny. The only reason why we wrote this code was for a small one-page site which where we didn’t want users to peek around too much. Some folks are just curious and they start running search or checking out feeds etc. So we disabled search, feeds, and redirected 404 to the homepage.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.