Ah, the dreaded 404… it’s amazing how frustrating these simple errors can be. It’s happened to the best of us. 404s love to pop up when a page within Wordpress change or the whole site is restructured, and users are left wondering, “why me?!” While there are many plugins that will allow you to deal with redirects, doing it yourself programmatically actually offers some unique advantages. Plus, you get bragging rights. 

Programmatic redirects really shine when you have bulk redirects or when you need a bit of customization to your redirects. 

Why Redirect Programmatically

Your site is newly revamped and you’re ready to go live. But first, you need to redirect hundreds of URLs to their new ones. Manually adding them one by one through a plugin is not for the faint of heart. This is where a programmatic method within Wordpress that makes good sense. With batch processing, you can code your redirect rules and get all of your redirects done in one go. 

Beyond the power of batch processing, programmatic redirects are also quite useful when your redirects need a bit of customization. 301s and 302s are important bits of information for search engines, and programmatically setting your redirects gives you the ability to define the redirect type. 

Even fancier than 301s and 302s, you can use conditional logic in your code to redirect only specific URLs or users. For example, you can implement a redirect on URLs that contain a certain parameter (“/param/123”). Or you can even redirect based on a user's location. This is an amazing level of control. 

Search Engines Eat 404s for Lunch

Too many 404s can really mess up your SEO. Not only are your users visiting your website, search engine crawlers are too. While they may be easy to forget about, they play a vital role in ensuring your brand gets the notice it deserves. When a crawler encounters a 404, typically it will take note and visit again. If a 404 is still there when the crawler revisits, the page is likely to be de-indexed. This means it will not appear in search engines, and your site’s SEO ranking just went down. 

Implementation: Redirect Based on a Parameter

Let’s take a look at some code. Here, we’re redirecting URLs that have a specific parameter. 

function my_parameter_redirect() {
    add_action('init', 'my_parameter_redirect_rule');
}
add_action( 'plugins_loaded', 'my_parameter_redirect' );
    
function my_parameter_redirect_rule() {
    
    // Define the parameter name and value to match
    $parameter_name = 'lang'; // Replace 'lang' with your actual parameter name
    $parameter_value = 'fr'; // Replace 'fr' with the value you want to redirect
    
    // Construct the redirect rule with pattern and destination
    $redirect_rule = '^/?(.*?)&' . $parameter_name . '=' . $parameter_value . '(.*)$';
    $redirect_target = '/fr/$1/'; // Replace '/fr/' with your target URL for the specific parameter value

    // Add the rewrite rule with optional arguments (top priority recommended)
    add_rewrite_rule( $redirect_rule, $redirect_target, 'top' );

    // Flush rewrite rules to ensure WordPress recognizes the new rule
    flush_rewrite_rules();

}

Here’s what we did in the code snippet:

  1. First we defined two functions. 
    1. my_parameter_redirect() hooks my_parameter_redirect_rule function to the init action.
    2. my_parameter_redirect_rule() defines the actual redirect rule.
  2. Then we defined the parameter name and value to match.
  3. $redirect_rule and $redirect_target matches the URLs that need to be redirected and defines the target URL, respectively.
  4. add_rewrite_rule( $redirect_rule, $redirect_target, 'top' ) adds the redirect rule to WordPress’s internal rewrite engine.
  5. And finally, flush_rewrite_rules() simply tells WordPress to updates its internal rewrite rules to reflect the new rule we created

While plugins are great and all, they don’t give you much control or flexibility over redirects. Programmatic redirects really do offer so much, from easily handling bulk redirects to adding custom conditional logic. And you can rest assured that your SEO score will remain high.