WordPress Redirect Visitor On 404 Error

WordPress shows a 404 page when visitor navigate to a non existent page. To redirect visitor to a specific page, say home page when visiting non existent page, edit functions.php file in your themes folder, then add following code to it.

function redirect_404_to_page() {
    if( is_404() ) {
        header( "Location: https://your-domain.tld/page/" , true, 404);
        exit;
    }
}
add_action( 'template_redirect', 'redirect_404_to_page' );
Add a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Advertisement