Redirect HTTP requests to HTTPS

This example demonstrates how to use URL redirects to redirect all requests from port 80 (HTTP) to port 443 (HTTPS).

HTTPS uses TLS (SSL) to encrypt HTTP requests and responses, making it safer and more secure. A website that uses HTTPS has https:// in the beginning of its URL instead of http://.

For existing load balancers

If you already have an external HTTPS load balancer (called here LB1) that is serving HTTPS traffic on port 443, you must create a partial external HTTP load balancer (called here LB2) with the following setup:

  • The same frontend IP address used by LB1
  • A redirect configured in the URL map

This partial HTTP load balancer uses the same IP address as your HTTPS load balancer and redirects HTTP requests to your load balancer's HTTPS frontend.

This architecture is shown in the following diagram.

HTTP-to-HTTPS redirect configuration architecture.
Figure 2. HTTP-to-HTTPS redirect configuration architecture

Redirecting traffic to your HTTPS load balancer

After you have verified that your external HTTPS load balancer (LB1) is working, you can create the partial external HTTP load balancer (LB2) with its frontend configured to redirect traffic to LB1.

This example uses the 301 response code. You can instead use a different response code.

To configure the redirect with gcloud, you must import a YAML file and make sure that your target HTTP proxy points to the URL map that redirects traffic. If you're using the Google Cloud console, this is handled for you.

Regional external HTTP(S) load balancers aren't supported in the Google Cloud console.


  1. Create a YAML file /tmp/web-map-http.yaml. This example uses MOVED_PERMANENTLY_DEFAULT as the response code.
  2.        kind: compute#urlMap
           name: web-map-http
             redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
             httpsRedirect: True
           - description: Test with no query parameters
             path: /test/
             expectedRedirectResponseCode: 301
           - description: Test with query parameters
             path: /test/?parameter1=value1&parameter2=value2
             expectedRedirectResponseCode: 301
  3. Validate the URL map.
  4. If the tests pass and the command outputs a success message, save the changes to the URL map.

  5. Create the HTTP load balancer's URL map by importing the YAML file. The name for this URL map is web-map-http.
  6. If you are updating an existing URL map, the following prompt appears:

           Url Map [web-map-http] will be overwritten.
           Do you want to continue (Y/n)?

    To continue, press Y.

  7. Verify that the URL map is updated. Your HTTP load balancer's URL map should look something like this:
  8. Create a new target HTTP proxy or update an existing target HTTP proxy, using web-map-http as the URL map.
  9. Create a forwarding rule to route incoming requests to the proxy. The --address flag specifies lb-ipv4-1, which is the same IP address used for the external HTTPS load balancer.

Testing the HTTP-to-HTTPS redirect

Note the reserved IP address that you are using for both load balancers.

In this example, assume that the reserved IP address is The URL redirects to

After a few minutes have passed, you can test this by running the following curl command.

curl -v

Sample output:

* Connected to ( port 80 (#0)
> GET / HTTP/1.1
> Host:
> User-Agent: curl/7.64.0
> Accept: */*
< HTTP/1.1 301 Moved Permanently
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Referrer-Policy: no-referrer
< Location:
< Content-Length: 220
< Date: Fri, 30 Jul 2021 21:32:25 GMT
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<H1>301 Moved</H1>
The document has moved
<A HREF="">here</A>.
* Connection #0 to host left intact