A regular expression (or regex) is a group of characters used to find a pattern within a piece of text.
In the context of Redirection, a plain URL match will match exactly one URL. A regex URL can match many URLs.
In addition to matching many URLs, a regular expression can extract information from the source URL and copy it to the target URL.
A few examples may be helpful. A redirect that has the source URL
/my-url will only ever match requests for
A redirect that has the source URL
/my-url/.* will match requests for:
And so on.
The important part of
.*. This is the regular expression part of the URL, and is equivalent to saying “match
/my-url/ followed by any sequence of characters”.
Regular Expression Syntax
So, regular expressions such as
.* seem really useful. But what does it actually mean?
In this instance the
. means ‘any character’, and the
* means any number of the previous expression. That is, any number of any characters.
But wait, it gets much much more complicated!
Regular expressions allow for very detailed and complex patterns that are beyond the scope of this page. If you want more details then search for ‘regular expressions’ and settle in for a long reading session.
Extracting Source Information
Not only can a regular expression match many URLs, it can also extract information from the source URL and copy it to the target URL.
Why would you want to do that? Let’s look at another example. Say you have a site where some pages are under the
/oldpage/ directory, and you’ve moved these to
And you want to move these to:
That is, you want to change
/newpage/, but keep
To do this you can create a regular expression such as
Note that the
.* is surrounded brackets. This tells Redirection to ‘capture’ the data. The target URL would then be
$1 is replaced by the contents of the captured
(.*). And so:
A frequently occurring problem with regular expression is an infinite redirect. That is, you create a regex that redirects to a URL that is itself caught by the same regular expression. This is then redirected again, and again, and again until the browser stops with a
ERR_TOO_MANY_REDIRECTS message (or equivalent).
For example, say we have this redirect:
If you access
/index.php/bananait will then be redirected to
/portal/index.php/banana. But wait! The URL
/portal/index.php/banana itself matches the original regular expression as
If we use the carat
^ character we can fix the match to the start of the URL:
^ tells the regular expression that it only applies when matched at the start of the URL. This prevents it matching elsewhere in the URL, and stops the infinite redirect.
Testing Regular Expressions
There are a lot of resources available for regular expressions, and some of the most useful are regular expression testers. With these you can experiment with a pattern and tune it to match exactly what you want.
Note that Redirection uses PHP’s regular expressions. These are commonly known as PCRE, and may not be exactly the same as other regular expression libraries.
A good resource to understand regular expressions can be found here.
Common Regular Expressions
The following regular expressions are commonly used for WordPress:
Date permalink to postname only:
Redirect all URLs to
/blog/ except ones that start with
Redirect every page on old site to new site:
Redirect .html pages in a directory: