How to Redirect 404 Page in WordPress without Plugin

How to Redirect 404 Page in WordPress without Plugin

Do you want to redirect the 404 page to the Homepage or another page? I am going to show “ How to Redirect 404 Page in WordPress without Plugin”.

404 page or not found page is a common page in WordPress. Redirecting the 404 page to the home help you reduce the bounce rate. It is also good for the broken link of your site.

There are many ways to redirect a 404 page to another page. In this article, I will show many of them.

Method 1: Template Redirect without Creating Template

If you don’t want to create a 404-page template.  Or you want complete this job with function.php. This way for you. Another hand, You can redirect 404 pages from the plugin in this way.

We will use WordPress hook template_redirect.

Use this code in function.php:

add_action( 'template_redirect', 'wpk_redreict_to_custom_404_page' );
function wpk_redreict_to_custom_404_page(){
    // check if is a 404 error
    if( is_404()  ){
        wp_redirect( home_url( '/' ) );
        exit();
    }
}

This function redirects your 404 error page to the homepage. Many developers use a custom page for 404 error pages. Let’s solve the problem.

You know a page has a ‘slug’. If you don’t know how to get page slug. I will make it easy for you.

See this code:

add_action( 'template_redirect', 'wpk_redreict_to_custom_404_page' );
function wpk_redreict_to_custom_404_page(){
    // check if is a 404 error
    if( is_404()  ){

    	$custom_pagename = 'Put your own custom page name'; //Put your own page name

		$page = get_page_by_title($custom_pagename);

		if($page != '' || !empty($page)){

			wp_redirect( home_url( '/'.$page->post_name.'/' ) );
        	exit();
		}else{
			wp_redirect( home_url( '/' ) );
	 		exit();
		}

        
    }
}

I expanded this function and added some lines of conditions. In this function, you will see a text named “Put your own page name” replace it with your custom page name or It will automatically redirect to the Homepage. 

Method 2: Redirect 404 Page With Template

This is a universal method most people use this method. In this way, you can redirect to another link like another domain or your homepage, etc. 

In this method i use php header() function.

Step 1: Create a page template named “404.php” in your current theme directory.

404 page template
Example of 404.php location

Stap 2: Open 404.php.

Step 3: Put this code on the 404.php file and save.

<?php
header("HTTP/1.1 301 Moved Permanently");

header("Location: ".home_url('/'));

exit();
?>

Then try to find something not found page.  I am sure it will redirect to the home page.

Conclusion:

If you have used a premium theme from ThemeForest don’t need to worry about the 404 page.

There are so many plugins in the WordPress plugin directory but I prefer not to use any plugin for such a normal task. I was already shown two types of ways to redirection the 404 page. Hope you already know about “ How to Redirect 404 Page in WordPress without Plugin” 

I hope you have liked this article. If this article helped you a little bit, Please comment below to encourage.

See More :

How to Create Social Share Button in WordPress without Plugin

One comment

Leave a Reply

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