How do I write a regex redirect in Rank Math?

The Citead team, updated

In short. Select Regex for the source pattern, add the destination with $1 for the first captured group, activate the rule and save it.

Definition: A regex redirect in Rank Math uses a regular expression, a text pattern, to select source URLs for redirection. A captured part of the source path can be inserted into the destination with a reference such as $1.

Key facts

  • As of October 2026, Rank Math's redirection setup guide requires Advanced Mode for the Redirections module to appear.
  • Rank Math's regex guide places the pattern in Source URLs and requires Regex as the matching option.
  • Rank Math's word-removal tutorial pairs ^wordtoreplace/(.*) with /$1 to remove a path prefix while retaining the remaining path.

Which matching mode applies?

Rank Math's setup guide describes these matching modes:

ModeMatching rule
Exact MatchThe requested URL matches the source URL exactly.
ContainsThe URL contains the text entered in Source URLs.
Start WithThe URL begins with the entered text.
End WithThe URL ends with the entered text.
RegexThe URL matches a regular expression pattern.

The guide illustrates Contains with oranges, Start With with bacon, and End With with orange. Match Type selects the URLs affected; Redirection Type sets the redirect's status code, such as 301 for a permanent move.

How is the rule entered?

Rank Math's word-removal tutorial gives this configuration:

Source URLs: ^wordtoreplace/(.*)
Match Type: Regex
Destination URL: /$1

The setup begins at WordPress Dashboard → Rank Math SEO → Redirections → Add New. The source and destination go in separate fields. The placeholder wordtoreplace is replaced with the prefix being removed. Status is set to Activate, then Add Redirection saves the rule.

The tutorial's example sends https://www.example.com/wordtoreplace/post-url/ to https://www.example.com/post-url/ with a 301 redirection. These addresses are the documentation's examples.

How do captured paths become destination paths?

A capturing group is a parenthesized part of a regular expression whose matched text can be reused. The PHP replacement documentation describes $n references and numbers capturing groups by their opening parentheses from left to right. $1 refers to the first group, $2 to the second.

In the prefix-removal rule above, (.*) captures the remaining path. The destination /$1 inserts that captured text after a slash.

Rank Math's permalink-change guide also gives an example with a full destination URL:

Source URLs: ^wordpress-tutorials/(.*)
Destination URL: https://example.com/$1

Its example maps https://example.com/wordpress-tutorials/install-plugin/ to https://example.com/install-plugin/. The domain in the destination is replaced with the site's own domain.

FAQ

Can I send every matching path to one fixed destination?

Yes. Rank Math's regex examples pair ^yoga/(.*) with https://yourdomain.com/shop/. That destination contains no captured-path reference, so the matched paths share the same destination.

How do I check which rule handles a URL?

Rank Math's setup guide documents Debug Redirections in the redirection settings. You can enable it and use the debugger's Manage This Redirection link to open the matching rule; the debugger is visible to website administrators only.

Sources

  1. Rank Math, "How to Set Up Redirections", undated
  2. Rank Math, "How to Use Regex Redirects in Rank Math", undated
  3. Rank Math, "How to Remove a Specific Word From Your Permalinks (URLs)", undated
  4. PHP, "preg_replace", undated
  5. Rank Math, "How to Redirect Posts After Changing URLs", undated

Check how a page reads to answer engines. Open the GEO score analyzer

Test a page and see what to fix first

The analysis usually takes less than 10 seconds and needs no account.

400,000+ pages scored in the public benchmark