BACK TO TOP

How to Redirect Your 404 Page to the Home Page in WordPress

How to Redirect Your 404 Page to the Home Page in WordPress

A website’s bounce rate is greatly influenced by how many 404 errors can occur. This can happen for various reasons: incorrect navigation links, or links to content which has been removed, or simply that the user has made a typo when trying to access a page from your website. And, while some 404 errors can be solved, all webmasters need to monitor and deal with those that remain.

You can deal with them by creating engaging 404 pages to improve the user experience or by redirecting the users back to the homepage so that they can quickly find what they were looking for. In this article, we will be discussing the latter approach. Specifically, we will explain the reasons why you might want to redirect your 404 page to the homepage in WordPress, as well as the methods how. This is what we will be talking about:

Qode Themes: Top Picks
Bridge New Banner
Bridge

Creative Multi-Purpose WordPress Theme

Stockholm WordPress Theme
Stockholm

A Genuinely Multi-Concept Theme

Startit WordPress Theme
Startit

Fresh Startup Business Theme

Pros And Cons of Redirecting Your 404 Page to the Homepage

404 error pages in the past have provided little value, as they didn’t give the reason why the visitor was seeing the 404 error screen and often didn’t include useful links the visitor could use to navigate around the website. That’s why visitors would often leave the website after seeing an error page.

Redirecting the 404 page to the homepage or some other page on the website was implemented in an attempt to keep the visitors on the website. Nowadays, this practice is less frequent as there are a lot of WordPress themes which offer stunning 404 pages that keep the visitors engaged while providing valuable website navigation. Nevertheless, redirecting the 404 page to the homepage can be useful in some cases.

As mentioned above, redirecting can help keep the traffic on the website, especially if your website has a lot of 404 errors. And, when redirecting your visitors, it is important to provide them with an explanation of why they were redirected there and a way to find the content that they were looking for. Observing the crawl errors in your Google Search Console account can provide insight for that explanation.

On the other hand, redirecting your 404 page comes with cons also. Unless provided the necessary explanation, users can get confused why they keep accessing your homepage when searching for specific content and leave the website as a result, regardless. Furthermore, Google interprets these redirections as soft 404 errors which could ultimately result in a loss of page rank. Furthermore, these kinds of website-wide redirections aren’t suggested for very large websites, as the number of redirect requests could significantly slow down the website.

All in all, redirecting your 404 page to the homepage can only be advised as a temporary measure. While a longer lasting solution should involve fixing the broken links on your website, creating an engaging and informative 404 page, logging the 404 errors, and applying redirections for specific URLs that get accessed or mistyped often.

How to Redirect Your 404 Page to the Homepage in WordPress Using a Plugin

Having understood why and when you might want to redirect your 404 page to the homepage, let us explore how you can do it. We will cover the plugin method first, as it is preferable for most WordPress users.

When it comes to redirections, there is a plethora of WordPress plugins, both free and premium, that you can choose from. Some were created specifically for redirection, or as part of a wider set of options included in an SEO plugin, for example. For this article, we have decided to cover the free WordPress redirection plugin which is slightly lesser-known, but ideal for this article. It is called Redirect 404 error page to homepage or custom page.

This simple plugin with a self-explanatory name was created specifically for adding and monitoring 404 error redirections. It is ideal for WordPress users whose theme offers an uninspiring 404 error page which can’t be changed without custom coding. Instead, using this plugin, you can set up the redirection rule from the 404 page to any other page on the website. Furthermore, you will get access to the 404 error log, so that you can properly monitor your 404 errors and find out why they occur. That said, let us explain how you can use the plugin.

After installing and activating the plugin, navigate to Redirect 404 > Settings. The options most relevant for this article are located in the Redirect Settings subsection. As such, select the home page from the dropdown menu of pages you wish to redirect to and choose the 301 redirect type. Then, optionally, you can enable 404 error logging by ticking the corresponding checkbox and setting the appropriate log history in days. Afterward, press the “Save” button in the top-right corner of the screen to save your option choices and apply the redirection.

Redirect 301

You should then test the functionality of the newly added redirection by trying to access a mistyped URL on your page. You should be redirected to your homepage, while the attempt will show in the Redirect 404 > 404 Log section if you have enabled 404 error logging previously.

404 Log

How to Redirect Your 404 Page to the Homepage in WordPress Using Code

Apart from using a WordPress plugin, you can redirecting 404 errors to the homepage using a custom code snippet. Needless to say, this method is better suited for intermediate and advanced WordPress users, as it requires a bit of previous coding experience and general WordPress knowledge. Nevertheless, we will try to explain it as thoroughly as possible using a code snippet we created specifically for the article so that it is understandable to all.

The code snippet included below should be added via FTP either in the functions.php file of your child theme or a suitable site-specific plugin. We will explain the former. But, if you opt to redirect your 404 page to the homepage using our code snippet, we suggest brushing up on your knowledge of FTP. Also, making a backup of your website as a precautionary measure is advised. Having done both, proceed below.

First, connect to your server using your FTP credentials and navigate to your root WordPress directory.

WordPress Directory

Then, navigate to the /wp-content/themes directory and locate the directory of your child theme. Click to open it and locate the functions.php file within. Right-click the file and select the “View/Edit” option from the menu.

WP Content View Edit

Afterwards, open the file in your preferred file editor and add the following code at the end of the file:

if( ! function_exists('qode_redirect_404_page_to_homepage') ){
function qode_redirect_404_page_to_homepage(){
if( is_404() ) {
wp_safe_redirect( home_url('/'), 301 );
exit;
}
}
add_action( 'template_redirect', 'qode_redirect_404_page_to_homepage' );
}
Add the Code

Then, save the changes you have made and upload the file back to the server, overriding the one currently there.

Let us elaborate on the code briefly.

The code represents a custom function called qode_redirect_404_page_to_homepage(). It redirects the user URL to the website’s homepage using the wp_safe_redirect() function. The homepage URL is specified using the home_url() function. Since the result of this function is the homepage URL without the trailing slash, we had to specify it as the parameter.

Thus, if you wanted to redirect the URL to some other page on the website, you would have to use ‘/relative-path-to-the-page’ as the parameter, with the relative-path-to-the-page replaced. For example, if you want to redirect to the About Us page (your-website-url/about-us), use ‘/about-us’ as the parameter.

The redirection status code we used is 301 (Moved Permanently), while redirection is performed only if the 404 page, i.e. an invalid or a page nonexistent on that website is accessed. To make all this possible, the code is hooked onto the template_redirect action hook.

This ensures that you don’t have to edit the code of the 404.php file directly, but can place the snippet inside the functions.php file of your child theme or a site-specific plugin, as previously stated. Thus, you will be able to properly keep track of all your coding modifications and not worry about losing them after updating the theme.

That said, after adding the code, the only thing that remains is to test the redirect functionality, i.e. try to access a mistyped URL from your page. Once you get successfully redirected to the homepage, the testing is complete.

Final Thoughts

Visitors are often inclined to leave a certain website after experiencing 404 errors on it. Webmasters have always needed a way to deal with these errors to avoid losing traffic and potential clients. Redirecting the user to the homepage after the 404 error has occurred is just one way of tackling the issue.

In this article, we have discussed two methods of adding such a redirection – using a plugin or a simple coding snippet. Since both methods are quite easy to implement, we are sure you were able to redirect your 404 page to the homepage successfully by following our instructions. And, since we have discussed the pros and cons of this method of dealing with 404 errors, you will know if you should implement it on your WordPress website.

Post your comment

Comments0