Question

I want to redirect all 404 error pages on my WordPress site to the homepage, or to a specific page.
Any advice?

Was it helpful?

Solution

Note: I have read somewhere that redirecting all 404 to Homepage is not a good approach for SEO purposes


Simplest way to redirect all 404 to homepage in wordpress.

if( !function_exists('redirect_404_to_homepage') ){

    add_action( 'template_redirect', 'redirect_404_to_homepage' );

    function redirect_404_to_homepage(){
       if(is_404()):
            wp_safe_redirect( home_url('/') );
            exit;
        endif;
    }
}

OTHER TIPS

It's probably a better idea to design your 404 page to include relevant information from your homepage and/or include a link to your homepage.

Simply redirecting all 404's to the home page can be confusing to users, and hurt your SEO / search engine ranking. It is suggested that you provide feedback to your users why a page they expected to see was not loaded. 404 error codes can serve a useful purpose.

If you do insist on doing blanket redirections, you should, at a minimum, track these redirects, so that you can fix errors.

This plugin that can do both Redirection, and more.

A great feature of this plugin, is that if you change the URL of a page within your site, you can specify a "permanent" redirect to the new proper home of your page.

If you monitor the 404's on your website, you can also identify mistakes, such as a typo in your html, that may be producing an unintended 404. You can then both fix the typo, and add a specific redirect to forward any requests for that mistyped link, to redirect to the correct url. Then visitors, whether using either link will arrive on the intended page.

You can also use the log to identify spelling mistakes that originate from typos on other sites linking to yours. Again, you can add a specific redirect that intercepts the typo and forwards them to the proper page. You could even try emailing them to inform them of their typo. They may or may not fix it, but either way, you are able to automatically direct users to the correct website.

Note, when you view the list of 404's logged by this (or any) plugin, you will see a ton of bad requests that are "bots" trying to find vulnerabilities in your website. These requests can either be ignored, redirected to a specific page (such as localhost, or your home page), or you can set a rule to kick them off your system after a certain number of bad requests.
For the most part, you will want to focus on 404's that are obviously the result of typos, or urls that once existed, but have either changed, or been removed altogether.

In the case of pages that have been deleted, then permanently redirecting those links to the home page may be the proper answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top