Rich and Robust WordPress Redirects

When migrating from one website to another, several steps may potentially be required. Here is an approach I recently used to redirect traffic to a new WordPress website while informing both human visitors and search engines of the move.

What Is Redirection?

Redirection on a website gives you a mechanism to forward visitors to another location on the same website or to another website. You would generally provide redirection if you have modified the location of content on your website or you are moving content to a new website.

For the most part redirection is something that is provided to tell Google and other search engines where you have moved content to. You can tell search engines that this redirection is temporary or permanent. These are also known as soft or hard redirects respectively.

When Google or another search engine attempts to index your site and they see a soft or hard redirect, they update their database of information. This effectively keeps the links on the search engine results page refreshed. If there wasn’t a way to do that, when you moved content, Google could potentially be pointing to a page that no longer exists.

WordPress Redirects

The duty of managing soft and hard redirects on a WordPress site generally falls to the search engine optimization plug-in being used on the website. While this is a great solution, managing redirection sometimes a more rich interactive and robust redirection is warranted.

Providing a more interactive redirect allows you to share useful information with visitors about why they are being redirected as well as any time-sensitive messages. This is a great way to remind your visitors to update their bookmarks.

To make redirection more rich and interactive, you can add an annoucement banner. I like to use Aakash Web’s Announcer plug-in; it is lightweight, fast, and plenty of features like countdown timers and animation in the paid version. You can indicate on the banner where the visitor is being redirected to, as well as how long before that happens. This is also a great time to indicate the new URL or website to bookmark.

Making the redirection more robust means writing some code. To add code to your WordPress site, you will either need to create a child theme and edit the functions.php or you will need a code snippet plug-in like WPCode or Code Snippets. If using one of these plug-ins, ensure the code is set to run everywhere.

The code we add will do the following:

  • Check to see if this the page is an admin page; if so, don’t do anything.
  • Set the new target domain.
  • Check page permalink to see if it maps to a new URL.
  • Build the URL for redirection.
  • Tell the browser (or search engine) that this resource has moved.
  • Redirect to new URL or website.
// add a new action
add_action('template_redirect', function () {
    if (is_admin()) {
        return; // Do not redirect admin pages
    }

    // Define the new domain
    $new_domain = 'https://mynewdomain.com';

    // List of specific pages and their new destinations
    $custom_redirects = [
        '/business-directory/' => 'https://mynewdomain.com/members/',    
        '/contact/' => 'https://mynewdomain.com/contact-us/',
		'/calendar/' => 'https://mynewdomain.com/corporate-events/',
    ];

    // Get the current request URI
    $current_path = $_SERVER['REQUEST_URI'];

    // Determine the redirect URL.  If the current path maps to one of the custom 
    // redirects above then redirect to the new destination.

    if (array_key_exists($current_path, $custom_redirects)) {
        $redirect_url = $custom_redirects[$current_path];
    } else {
        $redirect_url = $new_domain . $current_path;
        // Paths without custom redirect match pass through to the redirection.
        // redirect to base URL with $redirect_url = $new_domain;
    }

    // Send 301 Moved Permanently status.  This tells search engines to forget the old URL.
    // Set the refresh to the number of seconds to wait before redirecing to the new URL.
    header("HTTP/1.1 301 Moved Permanently");
    header("Refresh: 10;url=" . $redirect_url); 
});

Visitor Experience

Implementing a redirect with an SEO plug-in will ensure that search engine results remain fresh and accurate. It will even put in automatic redirection so that a web browser automatically goes to the new page. From a visitor’s or user’s point of view, a redirection without any information or prior notice can sometimes be confusing or disruptive.

A 5-10 second delay lets you inform your visitors about when, where, and why they are being redirected to a new URL. It’s a much nicer experience than being whisked away to a new website as soon as you arrive.

Summary

With just one or two plugins and a little bit of PHP code, you can provide redirections that are richly informative and highly robust in the complexity of redirection. You’ve seen just one way to redirect a set list of URLs to a new website. You can easily extend code like this using regular expressions to match and redirect a much larger set of pages.