The Redirection plugin for WordPress By John Godley is one of the most popular and powerful plugins on the repository. As a developer, I've used this plugin on pretty much every single project I've ever built.
TLDR: Regex when used for conditional/situational redirection is powerful, but hard. CodeWP now has an AI mode that will automatically convert your basic prompt into complex RegEx.
It allows you to add and manage redirections for a WordPress website without having to edit .htaccess or employ other methods. One of the best features of this plugin is the ability to implement RegEx, or regular expressions into the redirection rules. With this, you can create complex situational redirects based on the content in the source URL, and then pass it onto the target URL.
In this example, sourced from StackOverflow, the rule checks to see if the source URL has "big-brothers" and "amp", and if it does, removes amp and redirects to the new url.
It's not just this plugin that offers regular expression support. Yoast, Rank Math, SEOPress, and a host of other popular SEO & redirection plugins implement this powerful feature.
Like any other mode, you can load this from either the app page or the generator interface.
It should be noted that this mode is specifically tuned for the popular and free Redirection plugin. Therefore, it will create expressions that aren't actually accurate if you were to put them in regex testing tools. That's because we're prefacing all outputs with a carrot (^/), which prevents greedy expressions.
Then, all you need to do is prompt it for the redirect based on your specific needs, and CodeWP will output something like so:
Prompt:// If URL contains the word "dog" and "12/2022" anywhere in it, and the URL parameter of "redirect_dog_to_archive=true", redirect to "https://archive.demo.com".
To add to the plugin, open the Redirection settings screen. Then, click the add new button (1). Be sure to toggle on the regex option (2), paste in the source (3) and target URL (4), and finally clicked the add redirect button (5). Then test for your specific situation.
Be sure to realize that Regex can match and copy, but for complex modifications, you're better off using PHP (a good example can be found in our No-Plugin Membership Feature tutorial).
You'll notice that because were using AI, it can infer that Google has a specific domain name.
Take the prompt, "if url has parameter "redirect_to=google", redirect to google, but if it has "redirect_to=yahoo", redirect to yahoo."
You are unable to achieve this functionality with regex, as it simply isn't built for that.
Luckily, CodeWP has the general WordPress PHP mode, which can be used to achieve this type of redirection in a different way.
After swapping into that mode, and entering the same prompt, I get the following PHP, which will do exactly what I need: