Do you want to redirect your 404 page to your home page in WordPress?
Redirecting 404 ‘not found’ pages to your website’s home page can help reduce your bounce rate and give your users a chance to discover other useful content on your website.
In this article, we’ll show you how to redirect your 404 page to your home page in WordPress.
Why Redirect Your 404 Page to Your WordPress Home Page?
If you haven’t had the time to create a custom 404 page, then redirecting visitors to your home page can be a great solution.
When a user requests a page that doesn’t exist on your WordPress website, then WordPress will show them a 404 error page instead.
Most users who land on the default 404 page will leave your site quickly. This can increase your overall bounce rate and negatively impact your search engine rankings.
One way to keep your users engaged is by redirecting them to another more relevant page. Some website owners choose to take the shortcut and redirect users to the homepage.
In a perfect world, you would redirect specific 404 error requests to a more relevant page.
That being said, we’ll show you two ways you can redirect your 404 pages to your home page as well as custom pages in WordPress.
Method 1: Redirect All 404 Pages to the Home Page in WordPress (Advanced)
This method uses a simple code snippet to redirect all 404 pages to the homepage. If you want to see how to redirect individual 404 pages to more relevant pages, see method 2 in this article.
In this method, you need to add code to your WordPress files. If you haven’t done this before, then see our beginner’s guide on pasting snippets from the web into WordPress.
First, you’ll need to create a new file in your WordPress theme folder and name it 404.php
. If your theme already has a 404.php
file, then you’ll need to edit that file instead.
To edit this file, you need to connect to your WordPress hosting account with an FTP client or their file manager tool.
Once you’re connected to your website, you will be able to see your 404.php
file in your WordPress themes folder.
You need to add the following code as the first line in your 404.php
file:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();
?>
Next, you need to save the 404.php
file and reupload it to your theme directory using FTP or your hosting control panel.
Now, when a page is not found, instead of showing a 404 page, it will be redirected to your WordPress home page.
Method 2: Set Up Custom 404 Page Redirects With AIOSEO (Recommended)
The best and most SEO-friendly way of handling 404 errors is to redirect them to the most relevant page, not just your homepage.
This is where All in One SEO plugin comes in. It’s the best SEO plugin for WordPress used by over 2 million websites.
Its Redirects feature tracks all 404 page errors and lets you set up smart 404 page redirects in WordPress.
The first thing you need to do is install and activate AIOSEO. For more details, see our step-by-step guide on how to install a WordPress plugin.
Note: There is a free version of All in One SEO, but we’ll be using the Pro version since Redirect is a Pro addon.
Once the plugin is activated, you need to go to All in One SEO » Redirects. If you haven’t set up redirects yet, then you need to click the ‘Activate Redirects’ button.
Once you’ve done that, you need to click on the ‘Settings’ menu item and enable 404 logs.
Next, scroll down until you see the ‘Logs’ section and then click the ‘404 Logs’ toggle, so it turns blue.
Your 404 logs are a record of URLs that your visitors have tried to use to visit pages that no longer exist on your site.
You can choose how long you want the plugin to record your logs by selecting the time period from the drop-down menu.
Make sure you click ‘Save Changes’ before moving on.
After that, click on the ‘404 Logs’ menu option to see the last accessed 404 page. Remember, since you just enabled logging, it may not display anything right away. Give it up to 24 hours to see more useful insights there.
The ‘URL’ column will show you the 404 page the user tried to visit. You need to click the ‘Add Redirect’ link in the ‘Redirect’ column.
This will create a drop-down where you can enter your redirect information.
For best results, you need to enter the most relevant page to the original query in the ‘Target URL’ box and then click ‘Add Redirect’.
For example, if you had a tutorial on how to start a WordPress blog that you deleted and updated with a new one, then it’d be best to redirect that old URL to the new blog post rather than your homepage.
To add multiple 404 page redirects, simply follow the same steps as above.
Remember, generally you don’t want to redirect your 404 page to your home page forever. It’s a great temporary solution until you create a custom 404 page that will convert better or simply redirect users to a more relevant page.
We hope this article helped you learn how to redirect your 404 page in WordPress. You may also want to see our guide on the best drag and drop WordPress page builders to create custom 404 pages and our expert picks of the must have WordPress plugins for businesses.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Mark Cavallo says
very quick and easy trick – saved me a heap of time manualy redirecting with new website. thank you.
WPBeginner Support says
Glad our guide could help
Admin
Dave says
Please what if I don’t want it to go to the homepage, how do I set it to another custom page please?
WPBeginner Support says
We would recommend taking a look at our guide on tracking and redirecting 404s for other links below:
https://www.wpbeginner.com/plugins/how-to-track-404-pages-and-redirect-them-in-wordpress/
Admin
Mudassir says
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!
WPBeginner Support says
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
Admin
Mudassir says
Thank you, I replaced it and it works!
WPBeginner Support says
Glad it worked
Adeola says
Your posts are always very helpful. Thank you
Peter says
Is this going still to be shown as a 404 in the Google Search Console ?!
WPBeginner Support says
Hi Peter,
No, it will send 301 header message to crawlers telling them that the content has moved permanently.
Admin
Noble A. Ozogbuda says
Hello please how can i modify this code to redirect all my 404s pages to a specific page, not homepage?
Mentik says
Work Perfect … thanks
Jack says
It worked perfectly ! Thanks Syed
Mohan says
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.
Jeffrey Asamoah says
worked thanks!
Jitendra Mahato says
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.
Drake Bliss says
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,.
yudi cahyadi says
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..
umer tanveer says
Insert code to Main Theme
eray says
thank u so much. it works ..
Kaizur Ahmed says
Great! This code work fine.
Liem Hoang says
Work perfectly! Thank admin so much, you have been saved my life. My website will no longer get error win WMT.
Malindo My Id says
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
Shoaib says
i read anywhere on web that 404s should not be redirected globally to the home page.
whats your view ???
WPBeginner Support says
We agree that it is not the best practice. However, sometimes it may become necessary for some websites to do that.
Admin
Sanjar says
Thanks alot
Sid says
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?
WPBeginner Support says
Yes, it is possible. See plugins in our list of 6 best free 404 redirect plugins for WordPress.
Admin
Jaswinder says
Good and valuable information, this is what I was looking for.
Thanks.
Kumar says
This code is causing problem, when i post something new permalink gets shorten and does not contain complete post title.
What shld i do?
Miguel Guzman says
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.
Riccardo says
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
slango20 says
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
jhelum says
fantastic… thanks a lot
dcaryll says
Appreciate the quick explanation and answer. Thanks for the post.
Roy says
I am very grateful for this. You saved me doing 200+ redirects. Worked a treat.
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?
Vishal says
Thanks dude….
Prasanna says
I really thank full to you. My website here after won’t get crawl errors.
Thanks a lot.
slango20 says
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
marco says
Thanks that help a lot.
Dilpreet Bhatia Music says
Wow – That Was easy!!
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-
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.
Admin
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.
ud says
Thanks for your very useful information
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.
Admin
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.
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
Editorial Staff says
What is this extension called?
Admin
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.
Admin