Path
Rewrite path prefixes in requests by using the URLRewrite filter.
For more information, see the Kubernetes Gateway API documentation.
Before you begin
Follow the Get started guide to install Gloo Gateway.
Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.
Get the external address of the gateway and save it in an environment variable.
Rewrite prefix path
Use the HTTPPathModifier to rewrite path prefixes.
In-cluster services
Create an HTTPRoute resource for the httpbin app that configures an
URLRewritefilter to rewrite prefix paths. In this example, all incoming requests that match the/headersprefix path on therewrite.exampledomain are rewritten to the/anythingprefix path.Because the
ReplacePrefixPathpath modifier is used, only the path prefix is replaced during the rewrite. For example, requests tohttp://rewrite.example/headersare rewritten tohttps://rewrite.example/anything. However, for longer paths, such as inhttp://rewrite.example/headers/200, only the prefix is replaced and the path is rewritten tohttp://rewrite.example/anything/200.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-rewrite namespace: httpbin spec: parentRefs: - name: http namespace: gloo-system hostnames: - rewrite.example rules: - matches: - path: type: PathPrefix value: /headers filters: - type: URLRewrite urlRewrite: path: type: ReplacePrefixMatch replacePrefixMatch: /anything backendRefs: - name: httpbin port: 8000 EOFSetting Description spec.parentRefsThe name and namespace of the Gateway that serves this HTTPRoute. In this example, you use the httpgateway that was created as part of the get started guide.spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the URLRewritefilter is used.spec.rules.filters.urlRewrite.path.typeThe type of HTTPPathModifier that you want to use. In this example, ReplacePrefixMatchis used, which replaces only the path prefix.spec.rules.filters.urlRewrite.path.replacePrefixMatchThe path prefix you want to rewrite to. In this example, you replace the prefix path with the /anythingprefix path.spec.rules.backendRefsThe backend destination you want to forward traffic to. In this example, all traffic is forwarded to the httpbin app that you set up as part of the get started guide. Send a request to the httpbin app along the
/headerspath on therewrite.exampledomain. Verify that you get back a 200 HTTP response code and that your request is rewritten to the/anythingpath.Example output:
... "origin": "10.0.9.36:50660", "url": "http://rewrite.example:8080/anything", "data": "", "files": null, "form": null, "json": null ...Send another request to the httpbin app. This time, you send it along the
/headers/200path on therewrite.exampledomain. Verify that you get back a 200 HTTP response code and that your request path is rewritten to/anything/200.Example output:
... "origin": "10.0.9.36:50660", "url": "http://rewrite.example:8080/anything/200", "data": "", "files": null, "form": null, "json": null ...
External services
Create a Backend that represents your external service. The following example creates a Backend for the
httpbin.orgdomain.kubectl apply -f- <<EOF apiVersion: gateway.kgateway.dev/v1alpha1 kind: Backend metadata: name: httpbin namespace: default spec: type: Static static: hosts: - host: httpbin.org port: 80 EOFCreate an HTTPRoute resource that matches incoming traffic on the
/headerspath for theexternal-rewrite.exampledomain and forwards traffic to the Backend that you created. Because the Backend expects a different domain, you use theURLRewritefilter to rewrite the hostname fromexternal-rewrite.exampletohttpbin.org. In addition, you rewrite the/headerspath prefix to/anything.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: backend-rewrite namespace: default spec: parentRefs: - name: http namespace: gloo-system hostnames: - external-rewrite.example rules: - matches: - path: type: PathPrefix value: /headers filters: - type: URLRewrite urlRewrite: hostname: "httpbin.org" path: type: ReplacePrefixMatch replacePrefixMatch: /anything backendRefs: - name: httpbin kind: Backend group: gateway.kgateway.dev EOFSend a request to the
external-rewrite.exampledomain on the/headerspath. Verify that you get back a 200 HTTP response code and that the request was rewritten tohttpbin.org/anything.Example output:
* Request completely sent off < HTTP/1.1 200 OK HTTP/1.1 200 OK < content-type: application/json content-type: application/json < content-length: 268 content-length: 268 < server: envoy server: envoy < access-control-allow-origin: * access-control-allow-origin: * < access-control-allow-credentials: true access-control-allow-credentials: true < x-envoy-upstream-service-time: 2416 x-envoy-upstream-service-time: 2416 < { "args": {}, "data": "", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Host": "httpbin.org", "User-Agent": "curl/8.7.1", "X-Amzn-Trace-Id": "Root=1-68599cdc-5d3c0d9a1ac2aa482effb24b", "X-Envoy-Expected-Rq-Timeout-Ms": "15000", "X-Envoy-External-Address": "10.0.15.215", "X-Envoy-Original-Path": "/" }, "json": null, "method": "GET", "url": "http://httpbin.org/anything" }
Rewrite full path
Use the HTTPPathModifier to rewrite full paths.
In-cluster services
Create an HTTPRoute resource for the httpbin app that configures an
URLRewritefilter to rewrite prefix paths. In this example, all incoming requests that match the/headersprefix path on therewrite.exampledomain are rewritten to/anything.Because the
ReplaceFullPathpath modifier is used, requests tohttp://rewrite.example/headersandhttp://rewrite.example/headers/200both are rewritten tohttps://rewrite.example/anything.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-rewrite namespace: httpbin spec: parentRefs: - name: http namespace: gloo-system hostnames: - rewrite.example rules: - matches: - path: type: PathPrefix value: /headers filters: - type: URLRewrite urlRewrite: path: type: ReplaceFullPath replaceFullPath: /anything backendRefs: - name: httpbin port: 8000 EOFSetting Description spec.parentRefsThe name and namespace of the Gateway that serves this HTTPRoute. In this example, you use the httpgateway that was created as part of the get started guide.spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the URLRewritefilter is used.spec.rules.filters.urlRewrite.path.typeThe type of HTTPPathModifier that you want to use. In this example, ReplaceFullPathis used, which replaces the full path prefix.spec.rules.filters.urlRewrite.path.replaceFullPathThe path prefix you want to rewrite to. In this example, you replace the full prefix path with the /anythingprefix path.spec.rules.backendRefsThe backend destination you want to forward traffic to. In this example, all traffic is forwarded to the httpbin app that you set up as part of the get started guide. Send a request to the httpbin app along the
/headerspath on therewrite.exampledomain. Verify that you get back a 200 HTTP response code and that your request is rewritten to the/anythingpath.Example output:
... "origin": "10.0.9.36:50660", "url": "http://rewrite.example:8080/anything", "data": "", "files": null, "form": null, "json": null ...Send another request to the httpbin app. This time, you send it along the
/headers/200path on therewrite.exampledomain. Verify that you also get back a 200 HTTP response code and that the full path is rewritten to the/anythingpath.Example output:
... "origin": "10.0.9.36:50660", "url": "http://rewrite.example:8080/anything", "data": "", "files": null, "form": null, "json": null ...
External services
Create a Backend that represents your external service. The following example creates a Backend for the
httpbin.orgdomain.kubectl apply -f- <<EOF apiVersion: gateway.kgateway.dev/v1alpha1 kind: Backend metadata: name: httpbin namespace: default spec: type: Static static: hosts: - host: httpbin.org port: 80 EOFCreate an HTTPRoute resource that matches incoming traffic on the
external-rewrite.exampledomain and forwards traffic to the Backend that you created. Because the Backend expects a different domain, you use theURLRewritefilter to rewrite the hostname fromexternal-rewrite.exampletohttpbin.org. In addition, you rewrite any existing paths to/anything.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: backend-rewrite namespace: default spec: parentRefs: - name: http namespace: gloo-system hostnames: - external-rewrite.example rules: - filters: - type: URLRewrite urlRewrite: hostname: "httpbin.org" path: type: ReplaceFullPath replaceFullPath: /anything backendRefs: - name: httpbin kind: Backend group: gateway.kgateway.dev EOFSend a request to the
external-rewrite.exampledomain on the/headerpath. Verify that you get back a 200 HTTP response code and that the request was rewritten tohttpbin.org/anything.Example output:
* Request completely sent off < HTTP/1.1 200 OK HTTP/1.1 200 OK < content-type: application/json content-type: application/json < content-length: 268 content-length: 268 < server: envoy server: envoy < access-control-allow-origin: * access-control-allow-origin: * < access-control-allow-credentials: true access-control-allow-credentials: true < x-envoy-upstream-service-time: 2416 x-envoy-upstream-service-time: 2416 < { "args": {}, "data": "", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Host": "httpbin.org", "User-Agent": "curl/8.7.1", "X-Amzn-Trace-Id": "Root=1-68599cdc-5d3c0d9a1ac2aa482effb24b", "X-Envoy-Expected-Rq-Timeout-Ms": "15000", "X-Envoy-External-Address": "10.0.15.215", "X-Envoy-Original-Path": "/" }, "json": null, "method": "GET", "url": "http://httpbin.org/anything" }
Regex rewrite
Use a GlooTrafficPolicy to rewrite full paths with a regular expression (regex). The regex pattern and substitution that you define must follow the RE2 syntax.
In-cluster services
Create an HTTPRoute resource that routes incoming requests on the
rewrite.exampledomain to the httpbin app.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-rewrite namespace: httpbin spec: parentRefs: - name: http namespace: gloo-system hostnames: - rewrite.example rules: - matches: - path: type: PathPrefix value: / backendRefs: - name: httpbin port: 8000 EOFCreate a policy to define your path rewrite rules by using regular expressions. The following GlooTrafficPolicy finds a path that matches the
^/anything/stores/(.*)/entitiespattern, such as:- Allowed:
/anything/stores/us/entitiesor/anything/stores/dummy/entities - Not allowed:
/anything/stores/us/buildingsor/get/stores/us/entities
Note that the
patternregex must match the entire request path. For example, if you want to replace only a portion of the request path, you must still capture the entire path, such as with a/([^/]+)/([^/]+)/([^/]+)/([^/]+)pattern. If a matching path is found, it is rewritten to thesubstitutionvalue. In this example, the path is rewritten to the/anything/rewrite/path/ path.kubectl apply -f- <<EOF apiVersion: gloo.solo.io/v1alpha1 kind: GlooTrafficPolicy metadata: name: httpbin-rewrite-regex namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin-rewrite urlRewrite: pathRegex: pattern: "^/anything/stores/(.*)/entities" substitution: "/anything/rewrite/path" EOF- Allowed:
Send a request to the httpbin app along the
/anything/stores/us/entitiespath that matches the regex pattern in your GlooTrafficPolicy. Verify that you get back a 200 HTTP response code and that your request is rewritten to the/anything/rewrite/pathpath.Example output:
... "origin": "10.0.9.36:50660", "url": "http://rewrite.example/anything/rewrite/path", "data": "", "files": null, "form": null, "json": null ...Send another request to the httpbin app. This time, you send it along the
/anything/stores/us/buildingspath that does not match the regex pattern. Verify that you also get back a 200 HTTP response code and that the path is not rewritten to the/anything/rewrite/pathpath.Example output:
... "origin": "10.0.9.36:50660", "url": "http://rewrite.example/anything/stores/us/buildings", "data": "", "files": null, "form": null, "json": null ...
External services
Create a Backend that represents your external service. The following example creates a Backend for the
httpbin.orgdomain.kubectl apply -f- <<EOF apiVersion: gateway.kgateway.dev/v1alpha1 kind: Backend metadata: name: httpbin namespace: httpbin spec: type: Static static: hosts: - host: httpbin.org port: 80 EOFCreate an HTTPRoute resource that forwards traffic to the Backend that you created and rewrites the hostname from
external-rewrite.exampletohttpbin.orgthat is expected by the Backend.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: backend-rewrite namespace: httpbin spec: parentRefs: - name: http namespace: gloo-system hostnames: - external-rewrite.example rules: - filters: - type: URLRewrite urlRewrite: hostname: "httpbin.org" backendRefs: - name: httpbin kind: Backend group: gateway.kgateway.dev EOFCreate a policy to define your path rewrite rules by using regular expressions. The following GlooTrafficPolicy finds a path that matches the
^/anything/stores/(.*)/entitiespattern, such as:- Allowed:
/anything/stores/us/entitiesor/anything/stores/dummy/entities - Not allowed:
/anything/stores/us/buildingsor/get/stores/us/entities
Note that the
patternregex must match the entire request path. For example, if you want to replace only a portion of the request path, you must still capture the entire path, such as with a/([^/]+)/([^/]+)/([^/]+)/([^/]+)pattern. If a matching path is found, it is rewritten to thesubstitutionvalue. In this example, the path is rewritten to the/anything/ path.kubectl apply -f- <<EOF apiVersion: gloo.solo.io/v1alpha1 kind: GlooTrafficPolicy metadata: name: httpbin-rewrite-regex namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: backend-rewrite urlRewrite: pathRegex: pattern: "^/anything/stores/(.*)/entities" substitution: "/anything" EOF- Allowed:
Send a request to the
external-rewrite.exampledomain on the/anything/stores/us/entitiespath. Verify that you get back a 200 HTTP response code and that the request path and host were rewritten tohttpbin.org/anything.Example output:
* Request completely sent off < HTTP/1.1 200 OK HTTP/1.1 200 OK < content-type: application/json content-type: application/json < content-length: 268 content-length: 268 < server: envoy server: envoy < access-control-allow-origin: * access-control-allow-origin: * < access-control-allow-credentials: true access-control-allow-credentials: true < x-envoy-upstream-service-time: 2416 x-envoy-upstream-service-time: 2416 < { "args": {}, "data": "", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Host": "httpbin.org", "User-Agent": "curl/8.7.1", "X-Amzn-Trace-Id": "Root=1-68599cdc-5d3c0d9a1ac2aa482effb24b", "X-Envoy-Expected-Rq-Timeout-Ms": "15000", "X-Envoy-External-Address": "10.0.15.215", "X-Envoy-Original-Path": "/" }, "json": null, "method": "GET", "url": "http://httpbin.org/anything" }
Cleanup
You can remove the resources that you created in this guide.
kubectl delete httproute httpbin-rewrite -n httpbin
kubectl delete backend httpbin -n httpbin
kubectl delete GlooTrafficPolicy httpbin-rewrite-regex -n httpbin
kubectl delete httproute backend-rewrite -n httpbin