This example demonstrates how to use URL redirects to redirect all incoming requests to a different host or path.
Related content
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:
Configure a URL redirect for global external Application Load Balancers
Configure a URL redirect for regional external Application Load Balancers
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
- Go to your list of load balancers in the Google Cloud console.
Go to Load balancing - For a load balancer of type HTTP(S) (Classic), click the load balancer's name link.
- Click Edit .
- In Host and path rules, select Advanced host and path rule (URL redirect, URL rewrite).
- Click Add host and path rule.
- In the Hosts field, enter *.
- Click .
- For the Action, select Redirect the client to different host/path.
- For the host redirect, enter a hostname to redirect to, or omit to keep the requested host.
- For Path redirect, select Full path redirect.
- For Path value, enter a path to redirect to, or omit to keep the requested path.
- For Strip query, click Enable.
- Click Save to finish configuring the host and path rule.
- Click Done to finish editing the URL map.
- 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.
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
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
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