Wordpress

How to Redirect Pages or URLs in WordPress (Using without and with a plugin)

In WordPress, Redirect pages or URLs is very easy. If you have a WordPress website, sometime we delete WordPress posts or pages. When some of your website viewers visited this deleted post or pages, It will show a 404 not found error page. Also, If you have any 404 links in your blog, Google and other search engines won’t rank your post or page. 404 page is bad for SEO. So you will need to redirect these pages or URLs to other pages. Also read, How to Install Free SSL Certificate For Lifetime?

In this post, I will show you how to redirect pages or URLs in WordPress using a Plugin and without a plugin. Let’s begin

Redirect WordPress Pages or URLs using the Plugin

To redirect any pages or URLs in WordPress, Just install the Redirection plugin. Go to your website add a new plugin page and search Redirection. Then click on Install Now and Active

Redirection Plugin

After activating the plugin, Go to Tools > Redirection from the WordPress dashboard sidebar. Now you need to set up the Redirection plugin.

Redirection Start Setup

Click on the Start Setup button.

Redirection Basic Setup

Then you need to complete the basic setup. Now tick mark on both monitor permalink changes in WordPress posts and pages ad Keep a log of all redirects and 404 errors. Click on the Continue button. Then click on Finish Setup. It will take some time to process. Then click on Ready to begin. Setup Completed.

404 Page redirect

Now I want to redirect webcods.com/any-page to the homepage. So enter the Source URL as webcods.com/any-page and Target URL as my website homepage URL and the Group as Redirection. If you want to redirect the modified post, select Modified post from Group. Then click on Add Redirection. That’s it.

Now you could try to visit webcods.com/any-page. It will redirect you to the homepage directly.

Redirect WordPress Pages or URLs without a Plugin

2 ways you can redirect WordPress pages or URLs without a plugin. The first way is adding a redirect through the htaccess file and the second way is adding a function in the function.php. You can use both ways to redirect your WordPress pages or URLs.

Redirect Pages or URLs using .htaccess in WordPress

Redirecting pages or URLs using .htaccess is very easy but you have to be careful when you edit a .htaccess file. So you should need to download the .htaccess before edit and keep it safe on your computer to avoid any errors. To redirect the URL using .htaccess open your website control panel file manager. Then go to your website root directory.

edit htaccess file

Find the .htaccess file and download it. Then click on edit file and add the following line.

Redirect 301 /old-url /new-url

For permanent redirect use 301 and for temporary redirect use 302. An Example: I want to permanently redirect webcods.com/any-page to webcods.com/home

htaccess URL redirect

Redirect Page using function.php in WordPress

You can redirect your URL without a plugin easily by just adding a simple PHP function in themes function.php files. Just log in to your WordPress dashboard and go to Appearance > Theme File Editor. Then edit function.php and add the following code.

function redirect_page() {

     if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        $protocol = 'https://';
        }
        else {
        $protocol = 'http://';
    }

    $currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $currenturl_relative = wp_make_link_relative($currenturl);

    switch ($currenturl_relative) {
    
        case '/old-url':
            $urlto = home_url('/new-url');
            break;
        
        default:
            return;
    
    }
    
    if ($currenturl != $urlto)
        exit( wp_redirect( $urlto ) );


}
add_action( 'template_redirect', 'redirect_page' );

In this code, you just need to replace /old-url with the source URL and /new-url with the target URL.

An Example: We want to redirect webcods.com/any-page to webcods.com/home.

case '/any-page':
    $urlto = home_url('/home' );
    break;

If you want to redirect multiple URLs add this above code multiple times in function.php. I hope you understand how to redirect pages or URLs in WordPress. If you find this post useful, Please share our blog with your friend and neighbors.

If you need any help, you can contact me on Facebook.

Moin Uddin

Hi, I am Moin Uddin. A Web Developer from Bangladesh. I love to do coding and learn something new every day. You can contact me at [email protected]

Leave a Reply

Back to top button