WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
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

WPBeginner» Blog» Tutorials» How to Redirect your 404 page to the Home Page in WordPress

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

Last updated on September 11th, 2012 by Editorial Staff
32 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
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.

32 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

  • Revealed: Why Building an Email List is so Important Today (6 Reasons)

    Revealed: Why Building an Email List is so Important Today (6 Reasons)

  • How to Fix the Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • Google Analytics in WordPress

    How to Install Google Analytics in WordPress for Beginners

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

51 Comments

Leave a Reply
  1. Mudassir says:
    Aug 13, 2020 at 10:25 am

    Hello WPbeginner team,

    I am using Astra Pro and have already some code in my 404.php. Now my question is, where do I add the above code exactly? Should I remove all that existing code and add yours or how?

    Please help!

    Reply
    • WPBeginner Support says:
      Aug 13, 2020 at 10:59 am

      For this tutorial, you would replace the code in your 404 page with this code normally. As we state in the article, this should only be done in very specific cases

      Reply
  2. Adeola says:
    Dec 8, 2018 at 6:33 am

    Your posts are always very helpful. Thank you

    Reply
  3. Peter says:
    Nov 23, 2018 at 4:02 pm

    Is this going still to be shown as a 404 in the Google Search Console ?!

    Reply
    • WPBeginner Support says:
      Nov 23, 2018 at 5:57 pm

      Hi Peter,

      No, it will send 301 header message to crawlers telling them that the content has moved permanently.

      Reply
  4. Noble A. Ozogbuda says:
    Oct 19, 2018 at 6:08 am

    Hello please how can i modify this code to redirect all my 404s pages to a specific page, not homepage?

    Reply
  5. Mentik says:
    Sep 19, 2018 at 8:08 am

    Work Perfect … thanks

    Reply
  6. Jack says:
    Jun 23, 2018 at 4:03 am

    It worked perfectly ! Thanks Syed :)

    Reply
  7. Mohan says:
    Dec 14, 2017 at 1:41 am

    I have just installed it and did some redirection. I hope it works the way I expect. Anyway, its a nice plugin. Thanks for the helpful post about it.

    Reply
  8. Jeffrey Asamoah says:
    Oct 4, 2017 at 11:24 am

    worked thanks!

    Reply
  9. Jitendra Mahato says:
    Sep 14, 2017 at 5:49 am

    hi… i want to know that how to redirect the 404 page to a custom page.I want to do that in my blog. Thank you. Hoping your answer. I used this code and doing well.

    Reply
  10. Drake Bliss says:
    Mar 19, 2017 at 8:48 am

    If you only want to redirect a specific url path, try this code below. First create a child theme if you do not have one and move 404.php to the the child theme.

    Edit the 404.php page in the child theme and paste this code into the very top of the 404.php page. You can insert more check for other paths you want to redirect,.

    Reply
  11. yudi cahyadi says:
    Mar 8, 2017 at 12:21 pm

    hi there…i’m using chid theme but it doesn’t exist 404 file,, how to creat 404.php file using child theme not in main theme to insert code ..

    thank you..

    Reply
    • umer tanveer says:
      Sep 10, 2017 at 1:43 am

      Insert code to Main Theme

      Reply
  12. eray says:
    Dec 7, 2016 at 3:14 am

    thank u so much. it works ..

    Reply
  13. Kaizur Ahmed says:
    Nov 16, 2016 at 11:26 pm

    Great! This code work fine.

    Reply
  14. Liem Hoang says:
    Sep 28, 2016 at 7:15 am

    Work perfectly! Thank admin so much, you have been saved my life. My website will no longer get error win WMT.

    Reply
  15. Malindo My Id says:
    Jul 24, 2016 at 11:44 am

    wow thankyou so much,, did you know thats my site have been redirect from spammer, makes my eror page 404 found until 1240 not found url. and now I was redirect to my home age.. you are good boys.. thankyou.. I like this page,, so useful for many blogger ..

    Malindo My Id from Indonesia

    Reply
  16. Shoaib says:
    Jul 11, 2016 at 5:49 am

    i read anywhere on web that 404s should not be redirected globally to the home page.

    whats your view ???

    Reply
    • WPBeginner Support says:
      Jul 11, 2016 at 10:06 pm

      We agree that it is not the best practice. However, sometimes it may become necessary for some websites to do that.

      Reply
  17. Sanjar says:
    Jun 16, 2016 at 11:27 pm

    Thanks alot

    Reply
  18. Sid says:
    Jun 10, 2016 at 1:13 am

    Is there any way that I can redirect specific 404 pages. Like I created a custom post type and I just want to redirect 404 pages on that post type. Will that be possible?

    Reply
    • WPBeginner Support says:
      Jun 10, 2016 at 7:47 pm

      Yes, it is possible. See plugins in our list of 6 best free 404 redirect plugins for WordPress.

      Reply
  19. Jaswinder says:
    Apr 18, 2016 at 12:56 pm

    Good and valuable information, this is what I was looking for.

    Thanks.

    Reply
  20. Kumar says:
    Nov 19, 2015 at 8:19 am

    This code is causing problem, when i post something new permalink gets shorten and does not contain complete post title.

    What shld i do?

    Reply
  21. Miguel Guzman says:
    Jul 27, 2015 at 7:54 pm

    I have 1,500 404’s that is because i started using dates on the url at the beginning when i started using WordPress. I changed the permalinks and that’s when the 404 started. is really hard when you have so many redirect to map them individually. its easier to re direct then all to home page.

    Reply
  22. Riccardo says:
    May 12, 2015 at 12:08 pm

    How can you redirect depending on the language the site is displaying?
    My website is in french and english, but when I use your code it goes only to one page for both languages.

    Thank you for your help!

    Riccardo

    Reply
  23. slango20 says:
    Oct 17, 2014 at 8:25 pm

    this is bad practice, very bad. If you have time to set up a wordpress site, you have time to put 404 Not Found into a 404.php at *least* instead of redirecting to the homepage with no explanation whatsoever

    Reply
  24. jhelum says:
    Jul 16, 2014 at 5:03 am

    fantastic… thanks a lot

    Reply
  25. dcaryll says:
    Jun 9, 2014 at 2:41 pm

    Appreciate the quick explanation and answer. Thanks for the post.

    Reply
  26. Roy says:
    Apr 10, 2014 at 4:28 am

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

    Reply
  27. P.D. says:
    Mar 6, 2014 at 4:47 pm

    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?

    Reply
  28. Vishal says:
    Feb 23, 2014 at 10:04 pm

    Thanks dude…. :)

    Reply
  29. Prasanna says:
    Feb 20, 2014 at 6:41 pm

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

    Thanks a lot.

    :)

    Reply
    • slango20 says:
      Oct 17, 2014 at 8:29 pm

      those crawl “errors” are perfectly normal and don’t affect your site’s ranking, just like it’s not a good idea to reply to all sms with the same response just because you received a few spam sms

      Reply
  30. marco says:
    Feb 1, 2014 at 6:57 am

    Thanks that help a lot.

    Reply
  31. Dilpreet Bhatia Music says:
    Sep 11, 2013 at 2:43 am

    Wow – That Was easy!!

    Reply
  32. Filip Hajek says:
    Jun 13, 2013 at 3:41 am

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

    Reply
    • Eric says:
      Jul 2, 2013 at 11:34 am

      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-

      Reply
  33. Nikole says:
    Mar 2, 2013 at 11:54 am

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

    Reply
    • Editorial Staff says:
      Mar 4, 2013 at 6:50 am

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

      Reply
  34. Rakesh says:
    Feb 21, 2013 at 11:42 pm

    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.

    Reply
  35. ud says:
    Feb 14, 2013 at 12:29 am

    Thanks for your very useful information

    Reply
  36. Arslanh says:
    Sep 13, 2012 at 10:19 am

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

    Reply
    • Editorial Staff says:
      Sep 13, 2012 at 10:21 am

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

      Reply
  37. Julien says:
    Sep 12, 2012 at 10:12 am

    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.

    Reply
    • Rootpak says:
      Sep 15, 2012 at 7:45 am

      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.

      Reply
  38. Andy Feliciotti says:
    Sep 11, 2012 at 11:46 pm

    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

    Reply
    • Editorial Staff says:
      Sep 12, 2012 at 9:40 am

      What is this extension called?

      Reply
  39. Clark Wimberly says:
    Sep 11, 2012 at 12:10 pm

    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.

    Reply
    • Editorial Staff says:
      Sep 11, 2012 at 3:57 pm

      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.

      Reply

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
SeedProd Logo
SeedProd
Create beautiful custom landing pages - Drag & drop builder. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2020 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2020)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2020)
    • SiteGround Reviews from 4196 Users & Our Experts (2020)
    • Bluehost Review from Real Users + Performance Stats (2020)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2020 – Step by Step Guide
Deals & Coupons (view all)
MainWP
MainWP Coupon
Get 15% OFF on MainWP WordPress multisite manager plugin.
Sprout Invoices
Sprout Invoices Coupon
Get 50% OFF on Sprout Invoices WordPress invoicing plugin.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).
Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress CDN by MaxCDN | WordPress Security by Sucuri.