How to Redirect HTTP to HTTPS Without Losing Paths or Queries
Verify TLS first, then test HTTP-to-HTTPS redirects for apex and www, parameterized URLs and requests whose methods must survive the redirect.
- https
- redirects
- web-hosting
- guide
To move a site from HTTP to HTTPS, make HTTPS work on every hostname you will serve, then configure the HTTP listener to send each old URL to the intended HTTPS URL. Test the status and the exact destination. A working homepage does not establish that a bookmarked path, query or form still works.
Prove the destination is ready first
Visit the HTTPS version of the apex and www names directly. Each hostname that receives an HTTPS request needs a working TLS endpoint, even when its only job is redirecting to the other hostname. The certificate exchange takes place before the client can see the HTTP redirect.
Check the production certificate name and chain and confirm that a few real paths render normally. In a staging setup, also check for scripts, styles or images that still point at an insecure URL. Once the HTTPS endpoint is ready, configure the redirect at your host, load balancer or web server. DNS sends traffic to the right service; it does not set an HTTP redirect status.
Pick the status for the request you support
HTTP semantics distinguish temporary 307 from permanent 308. Both preserve the method when the client follows the redirect automatically. A client following 301 or 302 may change a POST to GET. For a permanently moved public page, Google treats both 301 and 308 as permanent redirects. For a form or API, update documented endpoints and clients to send directly to HTTPS. A redirect cannot retroactively protect sensitive data a client already sent over HTTP.
Check the path, query and final response
Suppose the intended site is https://example.com and both HTTP hostnames must land there. Use a request such as curl -sS -o /dev/null -D - 'http://www.example.com/docs/start?lang=en&source=guide'. Inspect the first status and the Location header without automatic following. Then check the final response separately with curl -sS -L -o /dev/null -w '%{url_effective} %{http_code}\n' 'http://www.example.com/docs/start?lang=en&source=guide'.
| Input | Expected first destination |
|---|---|
http://example.com/ | https://example.com/ |
http://www.example.com/docs/start?lang=en&source=guide | https://example.com/docs/start?lang=en&source=guide |
http://example.com/images/a%20b.png | https://example.com/images/a%20b.png |
https://www.example.com/docs/start | https://example.com/docs/start, after a valid TLS connection |
For any non-GET test, use an inert staging route and dummy data. Confirm what method the destination sees and verify that the request does not trigger a real action.
Finally, test the plain HTTP listener from a fresh client even if the browser appears to upgrade the URL automatically. HSTS directs a client that knows the policy to use HTTPS; it does not test the HTTP redirect that a different client might receive.
