Skip to content

networking: add prefix_rewrite and uri_regex_rewrite to HTTPRedirect#3708

Open
liamawhite wants to merge 3 commits into
istio:masterfrom
liamawhite:feature/redirect-prefix-rewrite
Open

networking: add prefix_rewrite and uri_regex_rewrite to HTTPRedirect#3708
liamawhite wants to merge 3 commits into
istio:masterfrom
liamawhite:feature/redirect-prefix-rewrite

Conversation

@liamawhite
Copy link
Copy Markdown
Member

@liamawhite liamawhite commented May 16, 2026

Summary

HTTPRedirect.uri performs a full path replacement and has no prefix-aware variant. Envoy's RedirectAction has supported prefix_rewrite and regex_rewrite for years, but Istio has never exposed them, making it impossible to perform redirects that preserve the path suffix (e.g. example.com/foo/barfoo.example.com/bar).

This was requested in istio/istio#47500, istio/istio#47777, and istio/istio#52521, all of which were closed as stale.

This PR adds two new fields to HTTPRedirect inside a oneof path_rewrite_specifier:

  • prefix_rewrite — replaces the matched route prefix, leaving the rest of the path intact. The route must use a prefix URI match.
  • uri_regex_rewrite — RE2 regex rewrite with capture group substitution (reuses the existing RegexRewrite message).

Both are mutually exclusive with the existing uri field (enforced by validation in istio/istio). The legacy uri field is unchanged.

Examples

Strip a prefix and change host (example.com/foo/barfoo.example.com/bar):

- match:
  - uri:
      prefix: /foo/
  redirect:
    authority: foo.example.com
    prefix_rewrite: /
    redirectCode: 301

Replace a prefix (example.com/foo/barexample.com/baz/bar):

- match:
  - uri:
      prefix: /foo
  redirect:
    prefix_rewrite: /baz
    redirectCode: 301

Regex rewrite — canonicalize account-scoped paths (example.com/accounts/12345/ordersexample.com/orders/12345):

- match:
  - uri:
      prefix: /accounts/
  redirect:
    uri_regex_rewrite:
      match: "^/accounts/([^/]+)/(.*)$"
      rewrite: "/\\2/\\1"
    redirectCode: 301

Full path replacement (existing behaviour, unchanged):

- match:
  - uri:
      prefix: /foo
  redirect:
    uri: /bar
    redirectCode: 301

Related

Extends HTTPRedirect to support prefix-aware path manipulation on
redirect responses, exposing Envoy's existing prefix_rewrite and
regex_rewrite capabilities in RedirectAction.

Previously, HTTPRedirect.uri replaced the entire path regardless of
the route match type. This adds two new mutually-exclusive fields:

- prefix_rewrite: replaces the matched route prefix with the given
  value, enabling /foo/bar -> /baz/bar style redirects
- uri_regex_rewrite: rewrites the path using RE2 regex with capture
  group substitution for complex transformations

Both are mutually exclusive with the existing uri field. Validation
is enforced in the istio/istio control plane.
@liamawhite liamawhite requested a review from a team as a code owner May 16, 2026 06:38
@istio-policy-bot
Copy link
Copy Markdown

🤔 🐛 You appear to be fixing a bug in Go code, yet your PR doesn't include updates to any test files. Did you forget to add a test?

Courtesy of your friendly test nag.

@istio-testing istio-testing added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 16, 2026
@keithmattix
Copy link
Copy Markdown
Contributor

I'm not sure why we shouldn't do this under the banner of gateway API (/cc @Stevenjin8). The fields will likely exist on VS since that's how we translate, but ideally Gateway API is the scope

@Stevenjin8
Copy link
Copy Markdown
Contributor

Fromt the title though, this is for http redirect, not path rewrites, so they are slightly different things.

@liamawhite
Copy link
Copy Markdown
Member Author

IIRC prefix rewrite for redirect exists in the gateway API but Istio implements it using a hack to get round the fact its not in the virtual service API.

i.e. Some metadata gets added during translation that allows the gateway API to pass this through to the thing generating the XDS configuration without it existing in the VirtualService API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants