Redirect Regular Expressions

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 /my-url.

A redirect that has the source URL /my-url/.* will match requests for:

  • /my-url/this
  • /my-url/that

And so on.

The important part of /my-url/.* is .*​. 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 /newpage/.

  • /oldpage/bananas/
  • /oldpage/best-post-in-the-world/

And you want to move these to:

  • /newpage/bananas/
  • /newpage/best-post-in-the-world/

That is, you want to change /oldpage/ to /newpage/, but keep bananas and best-post-in-the-world.

To do this you can create a regular expression such as /oldpage/(.*).

Note that the .* is surrounded brackets. This tells Redirection to ‘capture’ the data. The target URL would then be /newpage/$1.

Here the $1 is replaced by the contents of the captured (.*). And so:

/oldpage/bananas => /oldpage/(bananas) => /newpage/$1 => /newpage/bananas

Infinite Loops

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:

/index.php/(.*) => /portal/index.php/$1

If you access /index.php/banana​it will then be redirected to /portal/index.php/banana. But wait! The URL /portal/index.php/banana itself matches the original regular expression as index.php/banana matches /portal/index.php/(.*).

If we use the carat ^ character we can fix the match to the start of the URL:


Here the ^ 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:

Source: ^/\d{4}/\d{2}/\d{2}/(.*)
Target: /$1/

Example: /2017/01/01/thing/ => /thing/

Redirect all URLs to /blog/ except ones that start with /blog/:

Source: ^/(?!blog)(.*)
Target: /blog/$1

Example: /old-post-url/ => /blog/old-post-url/

Redirect every page on old site to new site:

Source: /(.*)

Example: =>

Redirect .html pages in a directory:

Source: /blog/(.*?)\.html
Target: /$1/

Example: =>

search previous next tag category expand menu location phone mail time cart zoom edit close