Articles on: WordPress

How To Setup A Page Redirect

Let's say you're checking out your Google search console statistics and you find that some users have clicked through to a non-existent page on your site like this:



Oh no! A bad user experience means an immediate bounce back to the search results, and a possible signal to Google that your site isn't giving users what they want.

The problem is that those pages don't even exist on your site! Or they used to exist at one point, but not anymore. Google unfortunately didn't get the memo and still sends visitors to your non-existent page. What do we do about it? The best thing is to use a 301 redirect to send users to another page that gives them what they're looking for. If it doesn't exist, create it! Obviously that page is doing something right by getting clicks in the SERPS. You need to capitalize on it.

Normally, this involves installing a plugin to redirect a page in WordPress. While that's not a bad idea, it's a bit excessive if you just want to redirect one or two pages. In this tutorial, I'll show you how to accomplish this without a plugin. Either using your functions.php file in your themes directory, or via a PHP code insertor.

So let's get started.

Step 1: Get the "From" URL Slug and the "To" URL Slug



Before we get into the code, you need to specifically write down two things:

Which URL needs to be redirected

To which URL should the users go to?

There are a few stumbling blocks here. First, check and see whether URLs on your site end with a "trailing slash" or not. In other words, are you this:

example.com/test-page


or this:

example.com/test-page/


To redirect a page in WordPress, we're going to be matching strings in code. It's extremely important to know exactly how your URLs are written.

Once you have the URLs, create the URL slugs. The "slug" is that part of the URL without the domain name, or https (or http).

So for the above example, the slug will be:

/test-page


Similarly if your URL is;

https://www.example.com/05/18/some-page


The slug will be:

/05/18/some-page


Note the initial slash "/" in front of the slug. This is going to be important.

Why Use Slugs and Not URLs?



In case you're wondering why we don't do a simple URL matching, the reason is simple. We want to insulate ourselves from potential changes. Maybe you create a subdomain for a page, or move your blog into a directory. Perhaps you're using the HTTP protocol and one day want to move to HTTPS.

Without the slug, all your attempts to match the URL will fail because the initial part is changing. But the slug is eternal. The slug is forever!

Step 2: Code in functions.php to Redirect a Page in WordPress



The second step is to open your "functions.php" file located in your theme (preferably a child theme). Now paste the following code into it:

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);
        echo $currenturl_relative;
    switch ($currenturl_relative) {

case '[from slug]':
            $urlto = home_url('[to slug]');
            break;

default:
            return;

}

if ($currenturl != $urlto)
        exit( wp_redirect( $urlto ) );
}
add_action( 'template_redirect', 'redirect_page' );


In this code, there are two sections in bold. Namely this one:

case '[from slug]:
    $urlto = home_url('[to slug]' );
    break;


This is where you enter your "from" and "to" slugs. Replace the sections in bold including the square brackets with the slugs you got from Step 1.

Save your changes, and now when you visit the "from" page, you should be redirected to the "to" page.

Step 3: Add More Redirects



I've structured the code in such a way that you can easily add more redirections if you want. For each new redirection, just place a new section like this under the first one:

case '[from slug]:
    $urlto = home_url('[to slug]' );
    break;
case '[from slug2]:
    $urlto = home_url('[to slug2]' );
    break;


Just make sure you keep the line "default" after all these "case" blocks and you should be fine. You can now redirect as many pages as you want to other pages without using a WordPress plugin!

Updated on: 10/10/2024

Was this article helpful?

Share your feedback

Cancel

Thank you!