Content MarketingSearch Marketing

New Domain Regular Expression (Regex) Redirects In WordPress

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

Their existing domain is staying put, but the new domain will have all content with respect to that product… from images, posts, case studies, downloads, forms, knowledge base, etc. We did an audit 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 to pull the switch and put it live had come. 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 key was setting up the redirects appropriately.

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/

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/(.*)

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/(.*)

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/(.*)

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

Will properly redirect to:

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

Will redirect to:

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!

Douglas Karr

Douglas Karr is the founder of the Martech Zone and a recognized expert on digital transformation. Douglas has helped start several successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to launch his own platforms and services. He's a co-founder of Highbridge, a digital transformation consulting firm. Douglas is also a published author of a Dummie's guide and a business leadership book.

Related Articles


What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.