Updated October 20, 2026
Get HeaderSwap for ChromeHome › 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
- Write an anchored pattern, for example
^https://www\.example\.com/(.*)$. - Write the target, for example
https://staging.example.com/\1. - Add it as a redirect rule in HeaderSwap and reload.
- 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.
| Token | Meaning |
|---|---|
^ 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
| # | Use | Pattern | Replacement | Input → Result |
|---|---|---|---|---|
| 1 | Production → staging | ^https://www\.example\.com/(.*)$ | https://staging.example.com/\1 | https://www.example.com/checkout?step=2 → https://staging.example.com/checkout?step=2 |
| 2 | CDN assets → localhost | ^https://cdn\.example\.com/assets/(.*)$ | http://localhost:8080/assets/\1 | https://cdn.example.com/assets/app.js → http://localhost:8080/assets/app.js (/img/logo.png not matched) |
| 3 | API v1 → v2 | ^(https://api\.example\.com)/v1/(.*)$ | \1/v2/\2 | https://api.example.com/v1/users/42 → https://api.example.com/v2/users/42 (v2 URLs not matched, so no loop) |
| 4 | Minified → debug bundle | ^(https://www\.example\.com/static/js/[a-z0-9-]+)\.min\.js$ | \1.js | …/js/main-3f9a.min.js → …/js/main-3f9a.js |
| 5 | Production API → local | ^https://api\.example\.com/(.*)$ | http://localhost:3000/\1 | https://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
- Open HeaderSwap and create a profile, for example "Staging redirect".
- Add a redirect rule.
- Limit the profile to the source domain, for example
www.example.com. - Grant site access if prompted, then reload.
Test and debug
- Open DevTools > Network, tick Preserve log, and reload.
- Find the original request. It should show a redirect to the new URL.
- If nothing happens, test the pattern on the exact URL from the Network tab, including query string and trailing slash.
- 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
| Problem | Fix |
|---|---|
| Rule never matches | Copy the exact URL from Network; check ^, $ and escaped dots |
| "Invalid" or rejected pattern | Remove lookaheads, backreferences and non-ASCII characters |
| Redirects to a broken URL | Check group numbers: \1 is the first parenthesis |
| Page loops or never loads | Target 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.
Related guides
- Modify request headers in Chrome: headers instead of URLs.
- Switch API environments with headers: Local, Staging and Prod profiles.
- Test CORS locally: redirecting to localhost often triggers CORS.
Add HeaderSwap to Chrome - Free
Sources
- chrome.declarativeNetRequest – Chrome for Developers (updated 2026-09-11): https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest
- RE2 Syntax – google/re2 wiki: https://raw.githubusercontent.com/wiki/google/re2/Syntax.md
Last verified: 2026-10-20
