Host Redirect

Gloo Gateway allows you to specify redirects instead of routes to a destination.

Setup

Before you begin, this guide assumes that you have the following setup.

  1. [Install Gloo Gateway]({{< versioned_link_path fromRoot="/installation/gateway/kubernetes" >}}) in the gloo-system namespace.
  2. Enable [discovery mode for Gloo Gateway]({{< versioned_link_path fromRoot="/installation/advanced_configuration/fds_mode/" >}}). If not, make sure that you created any Upstream resources with the appropriate functions.
  3. Install the glooctl command line tool.
  4. Identify the URL of the gateway proxy that you want to use for this guide, such as with the glooctl proxy command. Note that if you are testing in a local cluster such as Kind, you must use the custom localhost port that you configured instead of glooctl proxy, such as http://localhost:31500.

Creating a redirect virtual service

Let’s create a virtual service with a redirect action instead of a route action, redirecting “foo” to “google.com”:


apiVersion: gateway.solo.io/v1
kind: VirtualService
metadata:
  name: test-redirect
  namespace: gloo-system
spec:
  virtualHost:
    domains:
      - 'foo'
    routes:
      - matchers:
         - prefix: /
        redirectAction:
          hostRedirect: "google.com"

Now if we curl the route, we should get a 301 Permanently Moved response.

curl -v -H "Host: foo" $(glooctl proxy url)

This will contain the following message:

< HTTP/1.1 301 Moved Permanently
< location: http://google.com/

Summary

A virtual service route can be configured with a redirect instead of a routing action.

Let’s clean up the virtual service we created:


kubectl delete vs -n gloo-system test-redirect

glooctl delete vs test-redirect