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.
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!
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
Your posts are always very helpful. Thank you
Is this going still to be shown as a 404 in the Google Search Console ?!
Hi Peter,
No, it will send 301 header message to crawlers telling them that the content has moved permanently.
Hello please how can i modify this code to redirect all my 404s pages to a specific page, not homepage?
Work Perfect … thanks
It worked perfectly ! Thanks Syed
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.
worked thanks!
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.
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,.
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..
Insert code to Main Theme
thank u so much. it works ..
Great! This code work fine.
Work perfectly! Thank admin so much, you have been saved my life. My website will no longer get error win WMT.
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
i read anywhere on web that 404s should not be redirected globally to the home page.
whats your view ???
We agree that it is not the best practice. However, sometimes it may become necessary for some websites to do that.
Thanks alot
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?
Yes, it is possible. See plugins in our list of 6 best free 404 redirect plugins for WordPress.
Good and valuable information, this is what I was looking for.
Thanks.
This code is causing problem, when i post something new permalink gets shorten and does not contain complete post title.
What shld i do?
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.
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
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
fantastic… thanks a lot
Appreciate the quick explanation and answer. Thanks for the post.
I am very grateful for this. You saved me doing 200+ redirects. Worked a treat.
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?
Thanks dude….
I really thank full to you. My website here after won’t get crawl errors.
Thanks a lot.
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
Thanks that help a lot.
Wow – That Was easy!!
There is a cool WP plugin “404 Redirected”, whoch captures 404s for you and let’s you redirect them easily.
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-
What if I want t redirect to a specific page not the homepage?
You can change line 3. Or better use the redirection plugin like we mentioned in the article.
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.
Thanks for your very useful information
I did this but it doesn’t seem to work. I have no knowledge of coding so please help me!
It should work just fine if you follow the article how it is. This is not a very complicated piece of code.
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.
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.
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
What is this extension called?
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.
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.