This example demonstrates how to use [URL
redirects](https://docs.cloud.google.com/load-balancing/docs/url-map-concepts#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](https://docs.cloud.google.com/load-balancing/docs/https/setting-up-global-traffic-mgmt#configure_a_url_redirect)

- [Configure a URL redirect for
  regional external Application Load Balancers](https://docs.cloud.google.com/load-balancing/docs/https/setting-up-reg-traffic-mgmt#configure_a_url_redirect)

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](https://docs.cloud.google.com/load-balancing/docs/https/setting-up-http-https-redirect).

## Add a URL redirect

### Console

1. Go to your list of load balancers in the Google Cloud console.   
   [Go to Load balancing](https://console.cloud.google.com/networking/loadbalancing/list/loadBalancers)
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`](https://docs.cloud.google.com/sdk/gcloud/reference/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:

   ```yaml
   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`](https://docs.cloud.google.com/sdk/gcloud/reference/compute/url-maps/import) command:

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

## What's next

- [Clean up the load balancer setup](https://docs.cloud.google.com/load-balancing/docs/cleaning-up-lb-setup.md.txt).