RegEx for Redirection: A Reference Guide

Regular Expressions are very handy and save you a lot of time when you have the need to manipulate a lot of data in a little bit of time. When you have


multiplestrings where parts of them are the same, they are considered a pattern. Regular expressions can be built to match those patterns and manipulate them in many ways.

Meta Characters

A metacharacter is a character that has a special meaning (instead of a literal meaning) to a computer program, such as a shell interpreter or a regular expression engine. — For a full list of meta characters, visit

Examples of Meta Characters:

^     The root of the domain - only what is on the right of this will be matched.
.     (period) Match anything
\     First part used with a meta character (see examples below)
\d    Match only digits
\D    Match only non-digits
\w    Match only words
\W    Match any non-words
\s    Match any white space
\S    Match any non-white space
*     Match the character to the left and match it 0 or more times.
+     Match the character to the left one or more times

*Note: If there is no d, w, s (or other meta character) then the character proceeding the \ is treated literally. You can also escape a backslash; \d tells regex to treat it as a digit, however \\d tells regex to actually look for a \d (literally as a backslash and d).

As another example, if you have a directory with spaces in the name such as “directory name,” you would need to escape the space (escape means put \ in front of something) as follows:

directory\ name == “directory name”

Regular Expression Examples

== Strip “articles” out of the URL ==
Regex:        ^    /articles/(.*)/       (.*)
Variables:         /          $1 /        $2        /bees/bee-left-in-the-cold
Resulting URL:

== Strip out the .html from ==
Regex:     ^       /    (.*) \.html$
Variables:         /     $1   /
Resulting URL:

== The difference between / and not / ==

/title-of-post is not equal to /title-of-post/, however /title-of-post/*$ is equal to both of them.
If you are redirecting to, this is how you would do it in the redirection plugin:


== Detecting Digits ==

As you saw on the first page in the meta characters section, Regex can match digits.   2012   /     06     /   regex-is-amazingly-powerful   /
Regex:     ^   /   (\d*?) /   (\d*?)   /             (.*)                /    *$
Variables:     /     $1   /     $2     /              $3                 /

The output of the above would be just what the input is because I didn’t change anything – I just wrote down what everything was. However, if I wanted to display the URL above as, here’s how I would do it:     $3   /    $2      /              $1                 /    *$

Here’s what it would look like in the redirection plugin:

== Strip out part of a word ==

Occasionally you may have a WordPress category such as types-of-honey-1. Suppose that the -1 shouldn’t be in the category – the category is actually supposed to be types-of-honey. Now, suppose there are 1,241 articles in the types-of-honey-1 category now, yet all the links point to types-of-honey. This would be a nightmare without Regex.

Stripping out the -1 is very simple:        /    types-of-honey-1    /    clover-honey/
Regex:     ^               /        (.*)-1          /    clover-honey/
Variable:                  /         $1             /    clover-honey/
Result:    types-of-honey      /    clover-honey/

Comments are closed.

Post Navigation