CodeWP

Introducing The Redirection Regex Mode for CodeWP

By James LePage, CodeWP Founder
December 14, 2022
Contents

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.

How to Use

Like any other mode, you can load this from either the app page or the generator interface.

Toggle From App Page
Toggle From 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:

Source: ^(.*)dog(.*)redirect_dog_to_archive=true(.*)12/2022(.*) Target: https://archive.demo.com Generation ID: 7396

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.

Example Prompts

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).

Source

Prompt:// if url contains word "remove_this" replace it with "add_this".

Source: ^(.*)remove_this(.*)$ Target: $1add_this$2 Generation ID: 7410

Prompt:// if url has parameter "redirect_to=google", redirect to google.

You'll notice that because were using AI, it can infer that Google has a specific domain name.

. Source: ^(.*)redirect_to=google(.*) Target: https://google.com Generation ID: 7412

Prompt:// Remove .html from Blogger pages.

Source: ^/(.*?).html$ Target: /$1/ Generation ID: 7416

Prompt:// redirect pattern /?prodcategory=category-name&productid=12345 to /category_name/product/12345.

Source: ^/?prodcategory=(.*)&productid=(.*)$ Target: /$1/product/$2 Generation ID: 7418

Regex Is Limited In Some Situations

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:

function cwpai_redirect_to_google() { if (isset($_GET['redirect_to']) && $_GET['redirect_to'] == 'google') { wp_redirect('http://www.google.com'); exit; } } add_action('init', 'cwpai_redirect_to_google'); function cwpai_redirect_to_yahoo() { if (isset($_GET['redirect_to']) && $_GET['redirect_to'] == 'yahoo') { wp_redirect('http://www.yahoo.com'); exit; } } add_action('init', 'cwpai_redirect_to_yahoo'); Generation ID: 7413

Read More:

https://redirection.me/support/redirect-regular-expressions/

This new feature, and dozens more
php
PHP, JS or jQuery
select_all
AI models trained for WordPress & popular plugins
switch_access_shortcut
Unlimited generations
card_giftcard
100% free trial
Start For Free
link