New Domain Regular Expression (Regex) Redirects In WordPress | Martech Zone

For the last few weeks, we’ve been helping a client with a complex WordPress migration. The client had two products, both of which have become popular, so they had to split out the businesses, the branding, and the content into separate domains. It’s quite the undertaking!

Their existing domain is staying put, but the new domain will have all content related to that product, including images, posts, case studies, downloads, forms, a knowledge base, etc. We audited and crawled the site to ensure we wouldn’t miss a single asset.

Once we had the new site in place and operational, the time had come to pull the switch and put it live. That meant that any URLs from the primary site that belonged to this product had to be redirected to the new domain. We kept most paths consistent between the sites, so the key was setting up the redirects appropriately.

Introduction to Redirects Utilizing Regular Expressions

Managing website redirects is essential for optimizing the user experience, preserving SEO rankings, and preventing broken links that could harm your site’s credibility. For businesses and marketers managing large websites, handling redirects manually can become inefficient and error-prone. This is where Regular Expressions (Regex) shine as a powerful tool, enabling you to handle complex redirect rules with efficiency and precision.

By utilizing Regex, you can streamline the process of identifying URL patterns, making it easier to manage multiple redirects simultaneously with minimal effort. Whether you are redirecting URLs for SEO reasons, restructuring site architecture, or managing marketing campaigns, Regex provides an unmatched level of flexibility.

One of the main benefits of Regex in handling redirects is that it allows you to work with patterns instead of individual URLs. This means you can manage multiple redirects simultaneously rather than hard-coding every URL change. It is especially useful when your site undergoes structural changes or when you need to create bulk redirects in response to campaigns or SEO guidelines. Regex helps you maintain control over the complexity of URLs without overwhelming manual labor. Setting up the right rules can save work hours and ensure a more precise redirection system.

Short Lesson on Regular Expressions (Regex)

At its core, Regex is a sequence of characters that forms a search pattern. When combined with redirects, Regex allows you to match certain patterns of URLs and send them to the correct destination. Below is an overview of some key Regex components and their roles in URL management.

  • Literal Characters: These are the standard characters that match themselves (e.g., /products/, /blog/).
  • Anchors (^, $):
    • ^ matches the start of the string.
    • $ matches the end of the string.
      Example: ^/blog$ matches the exact URL /blog.
  • Wildcards (.): Matches any single character except newline.
    Example: /blog/.* will match any URL starting with /blog/.
  • Character Sets ([]): Matches any one character inside the brackets.
    Example: /product/[A-Za-z0-9]/ matches any URL that contains a product followed by a letter or number.
  • Quantifiers (*, +, ?, {}):
    • *: Matches 0 or more occurrences of the previous character.
    • +: Matches 1 or more occurrences.
    • ?: Matches 0 or 1 occurrence.
    • {n}: Matches exactly n occurrences.
      Example: /product/{3} matches exactly three occurrences of the word “product”.
  • Groups and Alternation ((), |):
    • () is used for grouping and capturing.
    • | represents alternation (OR).
      Example: /(blog|news)/ matches either /blog/ or /news/.

How the Destination URL Is Rebuilt

Once you have identified the pattern of the old URL using Regex, the destination URL can be dynamically rebuilt using backreferences. Backreferences allow you to reuse the captured parts of the matched pattern in the new URL.

For instance, let’s say you are migrating from an old blog structure to a new one:

  • Old URL: /blog/2023/my-old-article
  • New URL: /content/2023/my-old-article

Using Regex, you can capture the variable parts of the old URL and apply them to the new structure:

  1. Regex pattern for the old URL: /blog/(\d{4})/(.*)
  2. Redirect Rule for the new URL: /content/$1/$2

Here, $1 refers to the first captured group (the year) and $2 refers to the second captured group (the article name). This makes it possible to dynamically redirect any URL that matches the old pattern into the new structure, without manually mapping each URL.

By leveraging Regex in this way, businesses can create flexible, scalable, and efficient redirect rules that ensure a smooth transition between different URL structures.

Redirect Plugins in WordPress

There are two popular plugins available that do a great job of managing redirects with WordPress:

  • Redirection – perhaps the best plugin on the market, with regular expression capabilities and even categories for managing your redirections.
  • Rankmath SEO – this lightweight SEO plugin is a breath of fresh air and makes my list of Best WordPress Plugins on the market. It has redirects as part of its offering and will even import Redirection’s data if you migrate to it.

If you’re using a Managed WordPress Hosting engine like WPEngine, they have a module to handle redirects before the person ever hits your site… a pretty nice feature that can reduce latency and overhead on your hosting.

And, of course, you can write redirect rules into your .htaccess file on your WordPress server… but I wouldn’t recommend it. You’re one syntax error away from making your site inaccessible!

How To Create a Regex Redirect

In the example I provide above, it may seem simple to just do a typical redirect from a subfolder to the new domain and subfolder:

Source: /product-a/
Destination: 

There’s a problem with that, though. What if you have distributed links and campaigns that have a querystring for campaign tracking or referrals? Those pages won’t properly redirect. Perhaps the URL is:

Because you wrote an exact match, that URL won’t redirect anywhere! So, you may be tempted to make it a regular expression and add a wildcard to the URL:

Source: /product-a/(.*)
Destination: 

That’s pretty good, but there are still a couple of problems. First, it’s going to match any URL with /product-a/ in it and redirect them all to the same destination. So all of these paths will redirect to the same destination.



Regular expressions are a beautiful tool, though. First, you can update your source to ensure that the folder level is identified.

Source: ^/product-a/(.*)
Destination: 

That will ensure that only the primary folder level will redirect properly. Now for the second problem… how will you get the querystring information captured on the new site if your redirect doesn’t include it? Well, regular expressions have a great solution for that as well:

Source: ^/product-a/(.*)
Destination: $1

The wildcard information is actually captured and appended the destination by using the variable. So…

Will properly redirect to:

?utm_source=newsletter

Keep in mind that the wildcard will enable any subfolder to be redirected as well, so this will also be enabled:

features/?utm_source=newsletter

Will redirect to:

features/?utm_source=newsletter

Of course, regular expressions can get far more complex than this… but I just wanted to provide a quick sample of how to set up a wildcard regex redirect that passes everything cleanly to a new domain!

Incorporating Regex into your website’s redirect strategy allows for greater precision, time savings, and flexibility when managing large numbers of URLs. By matching and rebuilding URLs based on dynamic patterns, you can ensure that all redirects are handled efficiently without error-prone manual work.


Source: martech.zone