How to Redirects on a WordPress Website

Many people, including me, struggle with redirecting traffic from one page to another. If you’re looking for a quick and dirty way to redirect one page to another on your WordPress website, then look no further because here comes the good news: you can do it using only a few lines of code! This tutorial will teach you how.

The Basics

Before we get started, it’s important to note a few things about redirects. First, as the name would suggest, redirects are used to redirect traffic, or in other words, to direct visitors to a different URL. Second, redirects are nothing more than a simple text file in which you specify the URL that should be accessed once a visitor is redirected to your site. Third, WordPress uses HTTP Redirects which is a very common and easy to implement standard.

Setting Up Your Redirects

To set up your redirects, you’ll need to log into your WordPress dashboard, navigate to Settings and then to the Permalinks tab. In the example below, we’ve set the permanent and permalink (Permanent Post Name) of the installed Post Type to Redirection. For demonstration purposes, I’ve used the free Lets Encrypt certificate to secure my WordPress site.

As you can see, the only field that needs to be updated is the Permanent Post Name. This is the custom name that will be used to reference that particular page when someone clicks on a link, button, or image on your site. For example, the Home Page could be called “Home” or the About page could be called “About”.

Once you’ve set the name, you can click on the Save Changes button to finalize the settings.

Adding Your Redirects To Your Theme

Since we’ve already established that redirects are just a plain text file, we can now add them to our theme which will make it super easy to swap out content on a per-user basis. In your theme’s header, you’ll need to add a few lines of code that will look like this:

  • Redirects (just copy and paste)

    This is where we’ll place the redirects. Each line will start with Redirect::to(/) and then you’ll need to specify the URL that should be redirected to. In most cases, this will be the URL of a single page on your site. You can add as many redirects as you’d like.

    For example, you can have a Redirect to the Home Page, a Redirect to the About Page, a Redirect to a Product Page, etc. The possibilities are endless!

    To add a Redirect, navigate to your theme’s functions.php file and then add the following code:

    add_action('wp_head','redirect_links', 10, 0);
    function redirect_links() {
    $wp_title = get_the_title();
    if ($wp_title == "") {
    $wp_title = "Your Website Title";
    }
    ?>

    This will then search your site’s titles for the word “redirection” and if it doesn’t find any, it will create one. The reason we’re doing this is because WordPress uses the wp_head action to add and remove hooks which means that this function will be executed every time a web page is loaded into a browser. Since we want this function to run only when our redirects are added, we’ll make sure that they’re included in the header via the_content filter. To do this, add the following code:

    <!--#redirects {
    padding: 1em 0;}
    .redirection {
    font-weight: bold;
    text-align: center;
    color: #23282d;
    margin-top:.5em;
    margin-bottom:.75em;
    }
    -->

    Here we’re simply defining two sections, the first being the redirects section which is where we’ll place our Redirect tags and the second being the redirection section which is what we’ll use to style our redirects. Now when a visitor lands on one of our pages, they will be redirected to the URL that we specified in the code. In the example below, I’ve used the Home page but you can use whatever page you’d like.

    As you can see, we haven’t set a border around the div. Since we want the actual page content to be displayed and not the HTML that was behind the scenes, we’ll set the background color of the div to #fff and then we’ll use the overlay_script function to bring in the JavaScript that will make it all happen.

    The Finished Product

    Once you’ve added your redirects to your theme, you can go back to your WordPress dashboard and click on the Settings icon located at the top right. Then click on the Permalinks tab and you’ll see that your site’s permalinks have been updated.

    Now when you click on a link, button, or image on your site, it will redirect you to the specified URL.