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/ Destination: https://newdomain.com/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/(.*) Destination: https://newdomain.com/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.
https://existingdomain.com/product-a/ https://existingdomain.com/help/product-a/ https://existingdomain.com/category/parent/product-a/
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: https://newdomain.com/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/(.*) Destination: https://newdomain.com/product-a/$1
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!