Updated October 20, 2026

Get HeaderSwap for Chrome

Home › Extensions › HeaderSwap for HTTP Headers › Guides › Redirect URLs with regex in Chrome

How to Redirect URLs in Chrome With Regex Rules

By the Pick Ads team · Tested on Chrome · Published October 20, 2026 · Updated October 20, 2026

Answer: Chrome extensions redirect URLs with declarativeNetRequest rules. The pattern uses RE2 regex syntax, and the replacement uses \1 to \9 for captured groups. Anchor patterns with ^ and $, escape dots, and make sure the target can't match the pattern again. Limit the rule to your test domains, and turn it off when done.

Quick steps

  1. Write an anchored pattern, for example ^https://www\.example\.com/(.*)$.
  2. Write the target, for example https://staging.example.com/\1.
  3. Add it as a redirect rule in HeaderSwap and reload.
  4. Confirm the new URL in DevTools > Network.

Before you start

  • Time: 10 minutes.
  • You need: a staging or local server you control.
  • Free vs Premium: Free includes regex redirects, up to 10 rules per profile counting headers and redirects. Premium adds auto-expiry and current-tab-only profiles.

When redirects help

  • Load a production page against staging without changing code.
  • Point production assets (JS, CSS) to a local dev server to debug a live bug.
  • Try a new API version before the frontend switches.
  • Load unminified bundles to read stack traces.

Regex basics for Chrome rules

Chrome matches redirect patterns with RE2 syntax. The pattern must be ASCII only, and the host is matched in punycode. Matching is case-insensitive by default. In the replacement, \1 to \9 insert capture groups and \0 inserts the whole match (declarativeNetRequest reference, updated 2026-09-11).

RE2 does not support lookahead, lookbehind or backreferences inside the pattern (RE2 syntax). A pattern like ^https://(?!staging)… is rejected.

TokenMeaning
^ and $Start and end of the URL. Use both.
\.A literal dot. A bare . matches any character.
(.*)Capture "the rest", reused as \1
[a-z0-9-]+One or more of these characters

5 tested examples

#UsePatternReplacementInput → Result
1Production → staging^https://www\.example\.com/(.*)$https://staging.example.com/\1https://www.example.com/checkout?step=2 → https://staging.example.com/checkout?step=2
2CDN assets → localhost^https://cdn\.example\.com/assets/(.*)$http://localhost:8080/assets/\1https://cdn.example.com/assets/app.js → http://localhost:8080/assets/app.js (/img/logo.png not matched)
3API v1 → v2^(https://api\.example\.com)/v1/(.*)$\1/v2/\2https://api.example.com/v1/users/42 → https://api.example.com/v2/users/42 (v2 URLs not matched, so no loop)
4Minified → debug bundle^(https://www\.example\.com/static/js/[a-z0-9-]+)\.min\.js$\1.js…/js/main-3f9a.min.js → …/js/main-3f9a.js
5Production API → local^https://api\.example\.com/(.*)$http://localhost:3000/\1https://api.example.com/orders?id=7 → http://localhost:3000/orders?id=7 (api.example.com.evil.test not matched)

Examples 1 and 4 overlap: a minified-JS URL matches both. Give the more specific rule, example 4, a higher priority, or keep the two in separate profiles.

Set up in HeaderSwap

  1. Open HeaderSwap and create a profile, for example "Staging redirect".
  2. Add a redirect rule.
  3. Limit the profile to the source domain, for example www.example.com.
  4. Grant site access if prompted, then reload.

Test and debug

  1. Open DevTools > Network, tick Preserve log, and reload.
  2. Find the original request. It should show a redirect to the new URL.
  3. If nothing happens, test the pattern on the exact URL from the Network tab, including query string and trailing slash.
  4. Turn off other redirect or header extensions. If two extensions match the same request, Chrome's priority rules choose one.

Pitfalls: greedy patterns and loops

Unanchored, greedy patterns. In our test, example\.com/(.*) replaced with localhost:3000/\1 turned https://www.example.com/a?next=https://example.com/b into https://www.localhost:3000/a?next=…. Without ^, the pattern matched mid-URL and kept the https://www. prefix. Always anchor the pattern and include the scheme and host.

Loops. ^https://(.*)example\.com/(.*)$ → https://staging.example.com/\2 also matches its own target, staging.example.com, so the redirected URL qualifies again. Make sure the target can't match the pattern: name the source host exactly (example 1), or match a path the target doesn't have (example 3).

Unsupported syntax. Lookaheads and backreferences fail in RE2. Use a more specific host instead of "everything except".

Limits. An extension can have up to 1,000 regex rules, and each must compile under 2 KB.

Turn off when done

Pause the profile or switch to another one. With Premium, set auto-expiry to 1 hour, 24 hours or 7 days so a redirect can't silently persist. A forgotten production-to-local redirect looks exactly like an outage.

Troubleshooting

ProblemFix
Rule never matchesCopy the exact URL from Network; check ^, $ and escaped dots
"Invalid" or rejected patternRemove lookaheads, backreferences and non-ASCII characters
Redirects to a broken URLCheck group numbers: \1 is the first parenthesis
Page loops or never loadsTarget matches the pattern; narrow the host

What HeaderSwap does not do

It doesn't proxy or mock responses, and it can't use regex features RE2 lacks. It also doesn't change DNS: the target server must be reachable.

Last verified: 2026-10-20

Privacy: rules are applied by Chrome. See the HeaderSwap privacy policy.

FAQ

Does it change the address bar?

Can I redirect only one tab? With Premium, you can apply a profile to the current tab only. Chrome supports tab-specific rules only as session rules, which are cleared when the browser shuts down. On Free, profiles apply to every tab that matches the filter, so keep the pattern specific to your test host.

Why isn't my rule matching? Usually it's an anchor or escaping issue, or the real URL differs: http vs https, a "www" prefix, a trailing slash or a query string. Patterns must be RE2-compatible and ASCII only. Another extension's rule may also take priority. Paste the exact URL from the Network tab into your test.

Add HeaderSwap to Chrome - Free

Sources

Last verified: 2026-10-20