This post will cover how to redirect 404 to homepage in WordPress.
It is quite simple. All WordPress installations do have 404 error page built-in with most of the themes however at times the owner of the website or even the users do not like to be on any error page.
In such case, here are the steps outlined for you to give seamless experience to your users by redirecting any error page to the home page.
Login to your dashboard.
Go to “Apperarance” -> “Editor” -> 404.php
Copy the code below in 404.php file and you’re done.
1 2 3 4 |
<?php header("HTTP/1.1 301 Moved Permanently"); header("Location: ".get_bloginfo('url')); exit(); ?> |
The code above tells the browser that the page a visitor is looking for no longer exists and hence the visitor is redirected to the homepage.
I don’t have a 404 template on my theme, but it does have a theme setting to redirect 404’s to either home page or blog. I will use this code on some of my other sites. Thanks for the tip.
Glad it helps, Scott. Nice to see you after quite some time.
Yeah, I’ve been busy working on other stuff, but I am coming around when I can to check what else you have for us.
It is always good to see you around, Scott. Keep in touch.
Have a great day ahead.
Are we replacing the current code included in the file and replacing it with the one you provided?
Apology for not explaining it better. You need to enter only the provided code in your 404.php file. No other code in that file is required.
so where in the file do you copy the code? into the first line? the second? where?
You have to just delete all the codes in your present 404.php file and enter the provided code in that file.
Thanks Keyur for this explanation. It is now clear on how to go about it. I really appreciate on these insights on redirecting the 404 to homepage.
Glad it helps Davis.
so much amazing stuff to learn. This is awesome. Am I reading this correctly? I think that you’re saying that if we change the code in the php file that any time a page shows up with a 404 error, it will be redirected to the home page?
Thanks for your kindness Kate.
You got it right. Whenever a page shows up with a 404 error, it will be redirected to the home page.
Hi Keyur,
Wow! Thanks for sharing this code for correcting the 404 error. It is certainly an issue with WordPress and pretty scary for newbies like me!
Your generosity is much appreciated,
Aneeta
Thanks Aneeta for your kind words.
Wow! Thanks very much Keyur for all of the wonderful tips…Love the idea of the ‘one stop shop’ for newbies…ME!
If you don’t mind I’d love to run a couple of links between our blog…so if anyone is wanting to know about ‘FIXES’ I can refer them on. Are you keen on swapping links Keyur…….I have to say if I’ve got any sticky question I pop back and ask ya.
Thanks heaps
Shane
Hey Shane, thanks for your comment and kind words.
As far as link swapping is concerned, I would stay away from it purely because of SEO purpose.
I didn’t knew that Keyur….really appreciate the advise…..
Shane
Thanks Shane for your comment.
Hi Keyur
I don’t know much about 404 error pages because I had started my blogging career recently.
But somewhere I read that If any blogger or writer deleted the existing web page then the URL of that web page becomes 404 URL and that page becomes 404 page.
It is also said that Google don’t like 404 page so I checked my site whether I had any 404 page or not then I found few of the 404 error pages on my site also.
But Now after reading this post, I can easily handle these pages.
Thanks for sharing such a helpful post with us. 😀
thanks Mr. Key. is it faster to load than 404 plugin?
I means https://wordpress.org/plugins/404-to-homepage-littlebizzy/