Regex Redirects

Pro plan

Redirect a whole group of old URLs with one rule: a regular expression matches the old address, and the parts it captures are reused in the new one.

Needs a Pro, Business or Agency license

Without an active paid license the Use Regular Expression option is hidden, and saved regex rules stop redirecting until the license is active again. Exact redirects keep working on every plan.

How ProRank matches a regex rule

  • Exact rules come first. ProRank looks for an exact redirect for the requested address. Only when there is none does it try your regex rules, highest priority first (rules with the same priority in the order they were created). The first rule that matches is used.
  • The pattern sees the path only. No domain, no http or https, and no query string. On a site installed in a subfolder, that folder is removed first. For a request to /blog/my-post?ref=newsletter, the pattern is tested against /blog/my-post.
  • Patterns need delimiters, as in PHP. Wrap the expression in #, /, ~, !, @, % or `. With # you do not need to escape the slashes of a path: #^/old/(.*)$#. Flags go after the closing delimiter, for example i to ignore letter case. A pattern can be up to 1,024 characters.
  • Captured groups go into the target as $1, $2 and so on. A group that did not take part in the match is replaced by nothing.
  • The target must start with a slash or be a full address (http or https). Write /$1, not $1.
  • The visitor's query string is not carried over to the target of a regex rule. Add any parameters you need to the target yourself.

Create a regex redirect

  1. Open the Redirect Manager and add a new redirect.
  2. Tick Use Regular Expression.
  3. Enter the pattern as the source URL, for example #^/old-category/(.*)$#.
  4. Enter the target, using the captured groups: /new-category/$1.
  5. Check the rule in the Test Your Pattern box below the option. Enter up to 20 paths, such as /old-category/my-post, using Add another URL for more, then select Test Pattern. For each path it shows whether the pattern matches, the captured groups, and where it redirects to. Enter paths rather than full URLs, because the live redirect only sees the path.
  6. Save the redirect.
Source: #^/old-category/(.*)$#
Target: /new-category/$1

/old-category/my-post  ->  /new-category/my-post

Regex syntax basics

PatternMeaningExample
.Any single charactera.c matches abc and a1c
*Zero or more of the item before itab* matches a, ab and abbb
+One or more of the item before itab+ matches ab and abbb
?The item before it is optionalcolou?r matches color and colour
^Start of the path^/blog matches /blog/post, not /old/blog
$End of the path\.html$ matches /page.html
( )Capture group, reused as $1, $2 in the target^/old/(.*)$ captures everything after /old/
[ ]One character from a set[0-9] matches any digit
[^/]+One or more characters that are not a slash[^/]+ matches one path segment, such as seo
|Either sidecat|dog matches cat or dog
\dAny digit\d{4} matches 2024
\wA letter, digit or underscore\w+ matches hello_world

Common regex redirect patterns

Remove a file extension

Source: #^/(.*)\.html$#
Target: /$1

/about.html  ->  /about

Date-based permalinks to a flat blog

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

/2024/03/15/my-post/  ->  /blog/my-post/

Move posts from one category folder

Source: #^/blog/category/([^/]+)/(.*)$#
Target: /topics/$1/$2

/blog/category/seo/guide  ->  /topics/seo/guide

Match a folder whatever its letter case

The i flag matches /Products/, /PRODUCTS/ and /products/ alike. ProRank cannot change the case of the text a group captures: $1 is inserted exactly as it was requested.

Source: #^/products/(.*)$#i
Target: /products/$1

/Products/Widget  ->  /products/Widget

An optional part of the path

When the optional group is missing, $2 is replaced by nothing, so both addresses land on a clean URL.

Source: #^/article/([^/]+)(/page/\d+)?/?$#
Target: /blog/$1$2

/article/seo-guide         ->  /blog/seo-guide
/article/seo-guide/page/2  ->  /blog/seo-guide/page/2

A language folder to a parameter

Source: #^/(en|es|fr)/(.*)$#
Target: /$2?lang=$1

/es/about  ->  /about?lang=es

What a regex rule cannot do

  • Change the domain, such as www to non-www or http to https. The pattern never sees the domain. Set your preferred address in WordPress under Settings, General, or on your web server.
  • Match or strip a query string. The query string is removed before matching, and it is not passed on to the target.
  • Change letter case of the captured text, as explained above.

Common mistakes

No delimiters

A pattern that starts with ^ is rejected with “Regex pattern should start with a delimiter”. Wrap it: #^/old/(.*)$#.

A full URL in the pattern

Never matches: #^https://www\.mysite\.com/old/(.*)$#
Matches:       #^/old/(.*)$#

Forgetting the anchors

Too broad: #/old/(.*)#     matches /old/page and /folder/old/page
Anchored:  #^/old/(.*)$#   matches /old/page only

Greedy matching

#^/blog/(.*)/(.*)$#     on /blog/2024/03/post: $1 = "2024/03", $2 = "post"
#^/blog/([^/]+)/(.*)$#   on /blog/2024/03/post: $1 = "2024",    $2 = "03/post"

An unescaped dot

#^/page.html$#    the dot matches any character, so /pageXhtml matches too
#^/page\.html$#   matches /page.html only

Keep regex rules fast

Every active regex rule is tried, in priority order, on each request that has no exact redirect, until one matches. ProRank caps how much work a single pattern may do, so a runaway pattern stops early instead of hanging the request, but a long list still adds work to every unmatched request.

  • Anchor every pattern with ^ and $.
  • Prefer a specific class such as [^/]+ to .*.
  • Give specific rules a higher priority than broad catch-all rules.
  • Merge rules that differ only in one word: #^/(en|es|fr)/(.*)$# instead of three rules.

Import and export

A CSV export lists regex rules with the Regex column set to Yes. A CSV import creates regex rules only while a Pro, Business or Agency license is active. See Import and Export Redirects.