HTTP path redirects and rewrites

HTTPRoute resources can issue redirects to clients or rewrite paths sent upstream using filters. This guide shows how to use these features.

Note that redirect and rewrite filters are mutually incompatible. Rules cannot use both filter types at once.

Redirects

Redirects return HTTP 3XX responses to a client, instructing it to retrieve a different resource. RequestRedirect rule filters instruct Gateways to emit a redirect response to requests matching a filtered HTTPRoute rule.

Redirect filters can substitute various URL components independently. For example, to issue a permanent redirect (301) from HTTP to HTTPS, configure requestRedirect.statusCode=301 and requestRedirect.scheme="https":

  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: HTTPRoute
  3. metadata:
  4. name: http-filter-redirect
  5. spec:
  6. parentRefs:
  7. - name: redirect-gateway
  8. sectionName: http
  9. hostnames:
  10. - redirect.example
  11. rules:
  12. - filters:
  13. - type: RequestRedirect
  14. requestRedirect:
  15. scheme: https
  16. statusCode: 301

Redirects change configured URL components to match the redirect configuration while preserving other components from the original request URL. In this example, the request GET http://redirect.example/cinnamon will result in a 301 response with a location: https://redirect.example/cinnamon header. The hostname (redirect.example), path (/cinnamon), and port (implicit) remain unchanged.

HTTP-to-HTTPS redirects

To redirect HTTP traffic to HTTPS, you need to have a Gateway with both HTTP and HTTPS listeners.

  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: Gateway
  3. metadata:
  4. name: redirect-gateway
  5. spec:
  6. gatewayClassName: foo-lb
  7. listeners:
  8. - name: http
  9. protocol: HTTP
  10. port: 80
  11. - name: https
  12. protocol: HTTPS
  13. port: 443
  14. tls:
  15. mode: Terminate
  16. certificateRefs:
  17. - name: redirect-example

There are multiple ways to secure a Gateway. In this example, it is secured using a Kubernetes Secret(redirect-example in the certificateRefs section).

You need a HTTPRoute that attaches to the HTTP listener and does the redirect to HTTPS. Here we set sectionName to be http so it only selects the listener named http.

  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: HTTPRoute
  3. metadata:
  4. name: http-filter-redirect
  5. spec:
  6. parentRefs:
  7. - name: redirect-gateway
  8. sectionName: http
  9. hostnames:
  10. - redirect.example
  11. rules:
  12. - filters:
  13. - type: RequestRedirect
  14. requestRedirect:
  15. scheme: https
  16. statusCode: 301

You also need a HTTPRoute that attaches to the HTTPS listener that forwards HTTPS traffic to application backends.

  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: HTTPRoute
  3. metadata:
  4. name: https-route
  5. labels:
  6. gateway: redirect-gateway
  7. spec:
  8. parentRefs:
  9. - name: redirect-gateway
  10. sectionName: https
  11. hostnames:
  12. - redirect.example
  13. rules:
  14. - backendRefs:
  15. - name: example-svc
  16. port: 80

Path redirects

Path redirects use an HTTP Path Modifier to replace either entire paths or path prefixes. For example, the HTTPRoute below will issue a 302 redirect to all redirect.example requests whose path begins with /cayenne to /paprika:

  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: HTTPRoute
  3. metadata:
  4. name: http-filter-redirect
  5. spec:
  6. hostnames:
  7. - redirect.example
  8. rules:
  9. - matches:
  10. - path:
  11. type: PathPrefix
  12. value: /cayenne
  13. filters:
  14. - type: RequestRedirect
  15. requestRedirect:
  16. path:
  17. type: ReplaceFullPath
  18. replaceFullPath: /paprika
  19. statusCode: 302

Both requests to https://redirect.example/cayenne/pinch and https://redirect.example/cayenne/teaspoon will receive a redirect with a location: https://redirect.example/paprika.

The other path redirect type, ReplacePrefixMatch, replaces only the path portion matching matches.path.value. Changing the filter in the above to:

  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: HTTPRoute
  3. metadata:
  4. name: http-filter-redirect
  5. spec:
  6. hostnames:
  7. - redirect.example
  8. rules:
  9. - matches:
  10. - path:
  11. type: PathPrefix
  12. value: /cayenne
  13. filters:
  14. - type: RequestRedirect
  15. requestRedirect:
  16. path:
  17. type: ReplacePrefixMatch
  18. replacePrefixMatch: /paprika
  19. statusCode: 302

will result in redirects with location: https://redirect.example/paprika/pinch and location: https://redirect.example/paprika/teaspoon response headers.

Rewrites

Rewrites modify components of a client request before proxying it upstream. A URLRewrite filter can change the upstream request hostname and/or path. For example, the following HTTPRoute will accept a request for https://rewrite.example/cardamom and send it upstream to example-svc with host: elsewhere.example in request headers instead of host: rewrite.example.

  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: HTTPRoute
  3. metadata:
  4. name: http-filter-rewrite
  5. spec:
  6. hostnames:
  7. - rewrite.example
  8. rules:
  9. - filters:
  10. - type: URLRewrite
  11. urlRewrite:
  12. hostname: elsewhere.example
  13. backendRefs:
  14. - name: example-svc
  15. weight: 1
  16. port: 80

Path rewrites also make use of HTTP Path Modifiers. The HTTPRoute below will take request for https://rewrite.example/cardamom/smidgen and proxy a request to https://elsewhere.example/fennel upstream to example-svc. Instead using type: ReplacePrefixMatch and replacePrefixMatch: /fennel will request https://elsewhere.example/fennel/smidgen upstream.

  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: HTTPRoute
  3. metadata:
  4. name: http-filter-rewrite
  5. spec:
  6. hostnames:
  7. - rewrite.example
  8. rules:
  9. - filters:
  10. - type: URLRewrite
  11. urlRewrite:
  12. hostname: elsewhere.example
  13. path:
  14. type: ReplacePrefixMatch
  15. replacePrefixMatch: /fennel
  16. backendRefs:
  17. - name: example-svc
  18. weight: 1
  19. port: 80