How to redirect 404 error page to homepage in WordPress without Plugin

If you want to add some additional code to the above solution, you can use the following code snippet in your WordPress functions.php file.

Redirect to Home Page

// Redirect 404 Page into Home Page
function redirect_404_to_homepage() {
    if (is_404()) {
        wp_redirect(home_url(), 301);
        exit();
    }
}
add_action('template_redirect', 'redirect_404_to_homepage');

This code checks if the current page is a 404 error page using the is_404() function. If it is, it then redirects the user to the homepage using the wp_redirect() function with a 301 (permanent) status code. Finally, the exit() function is called to prevent any further processing of the page.

You can modify the redirection URL by changing the argument of the home_url() function to another URL of your choice.

Redirect to another Web URLĀ 

// Redirect 404 Page into Home Page
function redirect_404_to_homepage() {
    if (is_404()) {
        wp_redirect('https://www.hlomart.com', 301);
        exit();
    }
}
add_action('template_redirect', 'redirect_404_to_homepage');

you have to change url (https://www.hlomart.com)

Leave a Comment

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