With regex matching you can define a single source that matches many paths, and reuse parts of the matched URL in the destination. Available from the Pro plan and up.
How to use
Start the source with an exclamation mark (!) to mark its path as a regular expression:
!example.com/path/(.*)
The hostname is still matched literally. Only the path is treated as a regex, and it's matched against the path of the incoming request.
Example 1: redirect a whole section
Your new website no longer has a news section, and you want every old news URL to land on https://new-website.com/news-is-removed. Define the source as:
!example.com/news/(.*)
URLs that match:
https://example.com/news/test-123 https://example.com/news/other-article/sub-page-as-well
URLs that don't match:
https://example.com/other-page https://example.com/news
Example 2: match several prefixes
Your old website has paths that start with farming-, machine- and agriculture-, and you want to redirect only the first two:
!example.com/(farming|machine)-.*
URLs that match:
https://example.com/farming-page https://example.com/machine-page/sub-path-as-well
URLs that don't match:
https://example.com/agriculture-page https://example.com/other-page
Capture groups
Parts of the path wrapped in parentheses are captured and available in the destination as $1, $2 and so on, up to $10.
Example 1: forward the remainder of the path
Match everything after welcome/ and pass it on to the destination:
!example.com/welcome/(.*)
https://destination.com/$1
Example 2: restructure a dated URL
Match URLs that start with a date in year-month-day format followed by a slug, and rebuild them with a dash instead of a slash:
!example.com/^(\d{4}-\d{2}-\d{2})\/([\w-]+)$URLs that match:
https://example.com/2023-01-01/article-slug https://example.com/2022-06-02/another-article
URLs that don't match:
https://example.com/2023/article-slug https://example.com/article-slug https://example.com/20230101/article-slug https://example.com/2023-01-01/article-slug/extra-slug
Use both groups in the destination:
https://destination.com/news/$1-$2
The result for the first example is https://destination.com/news/2023-01-01-article-slug.
Capture groups are also available as parameters in dynamic destination expressions.
Regex engine
Regexes are evaluated with the Go RE2 engine. RE2 doesn't support lookahead, lookbehind or backreferences. The RE2 syntax reference lists what's available.
Matching order
When several regex sources exist on the same hostname, we try the most specific one first:
- We count the segments in each source path, separated by slashes. Slashes inside capture groups don't count.
- The source with the most segments (the deepest path) is tried first.
- Sources with the same number of segments are ordered by path length, longest first.
Read more about how sources are matched in Matching.
Testing your regex
Test your expression against example paths on regex101.com. Select the Golang flavor to get the same behavior as our engine.