Path redirects
Redirect requests to a different path prefix.
For more information, see the Kubernetes Gateway API documentation.
Before you begin
-
Follow the Get started guide to install kgateway.
-
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.
export INGRESS_GW_ADDRESS=$(kubectl get svc -n kgateway-system http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}") echo $INGRESS_GW_ADDRESSkubectl port-forward deployment/http -n kgateway-system 8080:8080
Set up path redirects
Path redirects use the HTTP path modifier to replace either an entire path or path prefixes.
Replace prefix path
-
Create an HTTPRoute for the httpbin app. In the following example, requests to the
/getpath are redirected to the/status/200path, and a 302 HTTP response code is returned to the user.Because the
ReplacePrefixPathpath modifier is used, only the path prefix is replaced during the redirect. For example, requests tohttp://path.redirect.example/getresult in thehttps://path.redirect.example/status/200redirect location. However, for longer paths, such as inhttp://path.redirect.example/get/headers, only the prefix is replaced and a redirect location ofhttps://path.redirect.example/status/200/headersis returned.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-redirect namespace: httpbin spec: parentRefs: - name: http namespace: kgateway-system hostnames: - path.redirect.example rules: - matches: - path: type: PathPrefix value: /get filters: - type: RequestRedirect requestRedirect: path: type: ReplacePrefixMatch replacePrefixMatch: /status/200 statusCode: 302 EOFSetting Description spec.parentRefs.namespec.parentRefs.namespaceThe name and namespace of the Gateway resource that serves the route. In this example, you use the Gateway that you created earlier. spec.hostnamesThe hostname for which you want to apply the redirect. spec.rules.matches.pathThe prefix path that you want to redirect. In this example, you want to redirect all requests to the /getpath.spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the RequestRedirectis used.spec.rules.filters.requestRedirect.path.typeThe type of path modifier that you want to apply. In this example, you want to replace only the prefix path. spec.rules.filters.requestRedirect.path.replacePrefixPathThe prefix path you want to redirect to. In this example, all requests to the /getprefix path are redirected to the/status/200prefix path.spec.rules.filters.requestRedirect.statusCodeThe HTTP status code that you want to return to the client in case of a redirect. For non-permanent redirects, use the 302 HTTP response code. -
Send a request to the httpbin app on the
path.redirect.exampledomain. Verify that you get back a 302 HTTP response code and thepath.redirect.example:8080/status/200redirect location.curl -vi http://$INGRESS_GW_ADDRESS:8080/get -H "host: path.redirect.example:8080"curl -vi localhost:8080/get -H "host: path.redirect.example"Example output:
* Mark bundle as not supporting multiuse < HTTP/1.1 302 Found HTTP/1.1 302 Found < location: http://path.redirect.example:8080/status/200 location: http://path.redirect.example:8080/status/200 < server: envoy server: envoy < content-length: 0 content-length: 0 -
Send another request to the httpbin app on the
path.redirect.exampledomain. This time, you send the request to the/get/headerspath. Verify that you get back a 302 HTTP response code and the redirect locationpath.redirect.example:8080/status/200/headers.curl -vi http://$INGRESS_GW_ADDRESS:8080/get/headers -H "host: path.redirect.example:8080"curl -vi localhost:8080/get/headers -H "host: path.redirect.example"Example output:
* Mark bundle as not supporting multiuse < HTTP/1.1 302 Found HTTP/1.1 302 Found < location: http://path.redirect.example:8080/status/200/headers location: http://path.redirect.example:8080/status/200/headers < server: envoy server: envoy < content-length: 0 content-length: 0
Replace full path
-
Create an HTTPRoute for the httpbin app. In the following example, requests to the
/getpath are redirected to the/status/200path, and a 302 HTTP response code is returned to the user.Because the
ReplaceFullPathpath modifier is used, requests tohttp://path.redirect.example/getandhttp://path.redirect.example/get/headersboth receivehttps://path.redirect.example/status/200as the redirect location.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-redirect namespace: httpbin spec: parentRefs: - name: http namespace: kgateway-system hostnames: - path.redirect.example rules: - matches: - path: type: PathPrefix value: /get filters: - type: RequestRedirect requestRedirect: path: type: ReplaceFullPath replaceFullPath: /status/200 statusCode: 302 EOFSetting Description spec.parentRefs.namespec.parentRefs.namespaceThe name and namespace of the Gateway resource that serves the route. In this example, you use the Gateway that you created earlier. spec.hostnamesThe hostname for which you want to apply the redirect. spec.rules.matches.pathThe prefix path that you want to redirect. In this example, you want to redirect all requests to the /getpath.spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the RequestRedirectis used.spec.rules.filters.requestRedirect.path.typeThe type of path modifier that you want to apply. In this example, you want to replace the full path.. spec.rules.filters.requestRedirect.path.replaceFullPathThe path that you want to redirect to. In this example, all requests to the /getprefix path are redirected to the/status/200prefix path.spec.rules.filters.requestRedirect.statusCodeThe HTTP status code that you want to return to the client in case of a redirect. For non-permanent redirects, use the 302 HTTP response code. -
Send a request to the httpbin app on the
path.redirect.exampledomain. Verify that you get back a 302 HTTP response code and the redirect locationpath.redirect.example:8080/status/200.curl -vi http://$INGRESS_GW_ADDRESS:8080/get -H "host: path.redirect.example:8080"curl -vi localhost:8080/get -H "host: path.redirect.example"Example output:
* Mark bundle as not supporting multiuse < HTTP/1.1 302 Found HTTP/1.1 302 Found < location: http://path.redirect.example:8080/status/200 location: http://path.redirect.example:8080/status/200 < server: envoy server: envoy < content-length: 0 content-length: 0 -
Send another request to the httpbin app on the
path.redirect.exampledomain. This time, you send the request to the/get/headerspath. Verify that you get back a 302 HTTP response code and the same redirect location as in the previous example (path.redirect.example:8080/status/200).curl -vi http://$INGRESS_GW_ADDRESS:8080/get/headers -H "host: path.redirect.example:8080"curl -vi localhost:8080/get/headers -H "host: path.redirect.example"Example output:
* Mark bundle as not supporting multiuse < HTTP/1.1 302 Found HTTP/1.1 302 Found < location: http://path.redirect.example:8080/status/200 location: http://path.redirect.example:8080/status/200 < server: envoy server: envoy < content-length: 0 content-length: 0
Cleanup
You can remove the resources that you created in this guide.kubectl delete httproute httpbin-redirect -n httpbin