Set up a URL redirect for a classic Application Load Balancer

This example demonstrates how to use URL redirects to redirect all incoming requests to a different host or path.

This page is for a classic Application Load Balancer only. If you are using a load balancer in a different mode, see one of the following pages:

If you're trying to set up an HTTP-to-HTTPS redirect, see Set up an HTTP-to-HTTPS redirect for a classic Application Load Balancer.

Add a URL redirect

Console

  1. Go to your list of load balancers in the Google Cloud console.
    Go to Load balancing
  2. For a load balancer of type HTTP(S) (Classic), click the load balancer's name link.
  3. Click Edit .
  4. In Host and path rules, select Advanced host and path rule (URL redirect, URL rewrite).
  5. Click Add host and path rule.
  6. In the Hosts field, enter *.
  7. Click .
  8. For the Action, select Redirect the client to different host/path.
  9. For the host redirect, enter a hostname to redirect to, or omit to keep the requested host.
  10. For Path redirect, select Full path redirect.
  11. For Path value, enter a path to redirect to, or omit to keep the requested path.
  12. For Strip query, click Enable.
  13. Click Save to finish configuring the host and path rule.
  14. Click Done to finish editing the URL map.
  15. Click Update to finish editing the load balancer.

gcloud

The following example returns a configurable 3xx response code, sets the Location response header with the appropriate URI, and replaces the host and path as specified in the redirect action.

  1. Export the existing URL map using the gcloud compute url-maps export command:

    gcloud compute url-maps export web-map-http \
      --destination=lb-map-config.yaml \
      --global
    
  2. Update the URL map file lb-map-config.yaml by adding this to the end of the file:

    hostRules:
    - hosts:
      - '*'
      pathMatcher: path-matcher-1
    name: web-map-http
    pathMatchers:
    - defaultUrlRedirect:
        hostRedirect: NEW_HOSTNAME # Omit to keep the requested host
        httpsRedirect: false
        pathRedirect: /NEW_PATH # Omit to keep the requested path
        redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
        stripQuery: true
      name: path-matcher-1
    
  3. Update the URL map using the gcloud compute url-maps import command:

    gcloud compute url-maps import web-map-http \
       --global \
       --source=lb-map-config.yaml
    

What's next