Host Redirect

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

Setup

This guide assumes that you have deployed Gloo to the gloo-system namespace and that the glooctl command line utility is installed on your machine. glooctl provides several convenient functions to view, manipulate, and debug Gloo resources; in particular, it is worth mentioning the following command, which we will use each time we need to retrieve the URL of the Gloo Gateway that is running inside your cluster:

glooctl proxy url

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