For more information, see the Kubernetes Gateway API documentation.

Before you begin

  1. Follow the Get started guide to install Gloo Gateway, set up a gateway resource, and deploy the httpbin sample app.

  2. Get the external address of the gateway and save it in an environment variable.

Set up path redirects

Path redirects use the HTTP path modifier to replace either an entire path or path prefixes.

Replace prefix path

  1. Create an HTTP route for the httpbin app that you set up as part of the Get started guide. In the following example, requests to the /get path are redirected to the /status/200 path, and a 302 HTTP response code is returned to the user.

    Because the ReplacePrefixPath path modifier is used, only the path prefix is replaced during the redirect. For example, requests to http://path.redirect.example/get result in the https://path.redirect.example/status/200 redirect location. However, for longer paths, such as in http://path.redirect.example/get/headers, only the prefix is replaced and a redirect location of https://path.redirect.example/status/200/headers is returned.

      kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1beta1
    kind: HTTPRoute
    metadata:
      name: httpbin-redirect
      namespace: httpbin
    spec:
      parentRefs:
        - name: http
          namespace: gloo-system
      hostnames:
        - path.redirect.example
      rules:
        - matches:
            - path:
                type: PathPrefix
                value: /get
          filters:
            - type: RequestRedirect
              requestRedirect:
                path:
                  type: ReplacePrefixMatch
                  replacePrefixMatch: /status/200
                statusCode: 302
    EOF
      
    SettingDescription
    spec.parentRefs.nameThe name and namespace of the gateway resource that serves the route. In this example, you use the gateway that you installed as part of the Get started guide.
    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 /get path.
    spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the RequestRedirect is 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 /get prefix path are redirected to the /status/200 prefix 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.
  2. Send a request to the httpbin app on the path.redirect.example domain. Verify that you get back a 302 HTTP response code and path.redirect.example:8080/status/200 redirect location.

    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
    < date: Mon, 06 Nov 2023 01:51:00 GMT
    date: Mon, 06 Nov 2023 01:51:00 GMT
    < server: envoy
    server: envoy
    < content-length: 0
    content-length: 0
      
  3. Send another request to the httpbin app on the path.redirect.example domain. This time, you send the request to the /get/headers path. Verify that you get back a 302 HTTP response code and the redirect location path.redirect.example:8080/status/200/headers.

    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
    < date: Mon, 06 Nov 2023 01:53:56 GMT
    date: Mon, 06 Nov 2023 01:53:56 GMT
    < server: envoy
    server: envoy
    < content-length: 0
    content-length: 0
      

Replace full path

  1. Create an HTTP route for the httpbin app that you set up as part of the Get started guide. In the following example, requests to the /get path are redirected to the /status/200 path, and a 302 HTTP response code is returned to the user.

    Because the ReplaceFullPath path modifier is used, requests to http://path.redirect.example/get and http://path.redirect.example/get/headers both receive https://path.redirect.example/status/200 as the redirect location.

      kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1beta1
    kind: HTTPRoute
    metadata:
      name: httpbin-redirect
      namespace: httpbin
    spec:
      parentRefs:
        - name: http
          namespace: gloo-system
      hostnames:
        - path.redirect.example
      rules:
        - matches:
            - path:
                type: PathPrefix
                value: /get
          filters:
            - type: RequestRedirect
              requestRedirect:
                path:
                  type: ReplaceFullPath
                  replaceFullPath: /status/200
                statusCode: 302
    EOF
      
    SettingDescription
    spec.parentRefs.nameThe name and namespace of the gateway resource that serves the route. In this example, you use the gateway that you installed as part of the Get started guide.
    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 /get path.
    spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the RequestRedirect is 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 /get prefix path are redirected to the /status/200 prefix 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.
  2. Send a request to the httpbin app on the path.redirect.example domain. Verify that you get back a 302 HTTP response code and the redirect location path.redirect.example:8080/status/200.

    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
    < date: Mon, 06 Nov 2023 01:55:14 GMT
    date: Mon, 06 Nov 2023 01:55:14 GMT
    < server: envoy
    server: envoy
    < content-length: 0
    content-length: 0
      
  3. Send another request to the httpbin app on the path.redirect.example domain. This time, you send the request to the /get/headers path. 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).

    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
    < date: Mon, 06 Nov 2023 01:56:06 GMT
    date: Mon, 06 Nov 2023 01:56:06 GMT
    < server: envoy
    server: envoy
    < content-length: 0
    content-length: 0
      

Cleanup

You can optionally delete the HTTP route that you created as part of this guide.

  kubectl delete httproute httpbin-redirect -n httpbin