Skip to content

You have installed an SSL certificate and the HTTPS version of your site opens without warnings. It is tempting to call the job done. But if a visitor types your domain without https://, clicks an old link in a newsletter or opens a bookmark from three years ago, the browser may still request the plain HTTP address. Unless your server answers that request with a redirect, the visitor lands on an unencrypted page, and search engines may index two versions of the same content.

A correct redirect is simple in principle and easy to get wrong in practice. This guide explains what a redirect does, which status code to use, how to configure it on common servers and behind a CDN, how to avoid loops and long chains, and how to test the result. After the redirect is in place you may find leftover insecure resources on your pages, which is the topic of what mixed content is and how to fix it.

What an HTTP to HTTPS redirect does

When a browser requests a page, the server answers with a status code. Most of the time it is 200, meaning "here is the page". A redirect uses a status code in the 3xx range together with a Location header that tells the client where to go next. The behaviour of these codes is defined in RFC 9110, HTTP Semantics.

For an HTTP to HTTPS redirect the flow looks like this:

  1. The browser asks for http://example.com/pricing.
  2. The server on port 80 replies with a permanent redirect and Location: https://example.com/pricing.
  3. The browser immediately requests the HTTPS address on port 443 and receives the page.

The visitor sees the secure address in the address bar and never has to think about it. Notice that the very first request still travels over HTTP. That is a limitation worth remembering and is the reason a further step, HSTS, exists; we come back to it near the end.

Choosing the status code

There are several redirect codes, but only a few matter here.

Code Meaning Method preserved? Typical use
301 Moved permanently Not always; some clients turn POST into GET Standard choice for moving whole sites to HTTPS
308 Permanent redirect Yes Permanent move where forms or API calls must keep their method
302 Found (temporary) Not always Short-term moves; not appropriate for a permanent switch
307 Temporary redirect Yes Temporary moves that must keep the method

For a permanent move to HTTPS, use a permanent code. A 301 is the most widely understood and is fine for ordinary page visits. If your site receives form posts or API calls on HTTP and you want them to arrive unchanged, a 308 is the more precise tool. Avoid using 302 for the switch, because it tells clients the move may be temporary, and they may keep requesting the HTTP address.

Be careful with testing. Browsers cache permanent redirects aggressively. While you experiment, you may prefer a temporary code or a private window, and switch to the permanent code when the rule is final.

Before you start: a checklist

  • The HTTPS version works for every hostname you serve. Open https://example.com and https://www.example.com and confirm that both show a valid certificate. If you see a name warning, fix it first with the guide to SSL certificate name mismatch.
  • You have decided on one canonical hostname, either with or without www.
  • You know where TLS ends. If a CDN, load balancer or hosting panel sits in front of your server, the redirect may belong there rather than on the origin.
  • You have a backup of the configuration file you are about to edit, and a way to undo the change.

Step by step on common setups

The exact syntax depends on your environment. The ideas below are the standard patterns. Adapt host names to your own and test on a staging copy where possible.

Apache with .htaccess

With mod_rewrite enabled, a common rule is:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The condition checks that the request did not arrive over HTTPS. The rule then sends the visitor to the same host and path over HTTPS. If you use a fixed canonical host, write it explicitly instead of %{HTTP_HOST}, so that stray host names do not slip through.

Nginx

In Nginx, the cleanest approach is a dedicated server block listening on port 80 that does nothing but redirect:

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

Using return is simpler and faster than a rewrite rule. A second server block listens on port 443 for the real site.

Hosting control panels and website builders

Many panels offer a switch such as "force HTTPS". Turn it on, then check the result with a test rather than trusting the label. Some switches only redirect the main domain and leave subdomains alone, and some add the redirect in a place that conflicts with another plugin.

WordPress and other CMS platforms

Set the site address and home address to https://, then add the server-level redirect. Relying only on a plugin is acceptable for small sites but adds work for every request. Do not stack two mechanisms that both redirect, such as a plugin and a server rule with slightly different targets, because that can create chains.

Behind a CDN or reverse proxy

If a CDN terminates TLS and talks to your origin over HTTP, the origin sees every request as plain HTTP. A naive "redirect if not HTTPS" rule at the origin then redirects forever, producing the error "too many redirects". The solution is either to let the CDN handle the redirect, or to make the origin look at the X-Forwarded-Proto header that the CDN sets and redirect only when the original visitor request was not HTTPS. Whichever you choose, put the rule in one place.

Avoiding loops and long chains

Two problems cause most redirect complaints.

Loops. The browser shows "too many redirects" or "ERR_TOO_MANY_REDIRECTS". The usual causes are the proxy situation above, a CMS that forces HTTPS while the server forces something else, or two rules that undo each other, for example one forcing www and another forcing non-www. Read the Location headers of each hop. The loop becomes obvious when you see the same two addresses alternate.

Chains. A chain happens when a visitor is sent through several hops, for example http://example.com to https://example.com to https://www.example.com. Each hop adds delay and gives search engines an extra step to follow. Aim for one hop: every combination of http, https, www and non-www should go directly to the final canonical address.

A worked example

Suppose your canonical address is https://example.com. You want four starting points to end in the same place:

Requested Desired result
http://example.com/page 301 to https://example.com/page
http://www.example.com/page 301 to https://example.com/page
https://www.example.com/page 301 to https://example.com/page
https://example.com/page 200, the page

On Nginx, the port-80 block above handles the first two in one hop, because it redirects both host names directly to the canonical HTTPS address. A small extra server block on port 443 for www.example.com returns a 301 to https://example.com$request_uri. Note that this block needs a certificate that covers www.example.com, which is another reason to check names first.

After this setup, you test each of the four requests. If any returns two hops, you adjust the rule until each is a single step.

How to test

You do not need special software.

  1. Use a command-line client that shows headers without following redirects. Request the HTTP address and read the first line (status code) and the Location header.
  2. Repeat for the www variant and for a deep URL with a query string, such as /shop?item=5. A good redirect preserves the path and query.
  3. Follow the chain with a tool that lists every hop. Count them.
  4. Open the site in a private browser window to avoid cached redirects.
  5. Check pages that use forms, login or an API, since those are where a wrong code or a lost query string shows up.
  6. Look at the browser console for mixed content messages once the page loads over HTTPS.

Limits and what to watch for

  • The first request is still HTTP. A redirect cannot protect that initial request. HSTS tells the browser to use HTTPS directly on later visits, but it needs care, which is why it deserves its own guide. Read it before you enable it.
  • Redirects do not fix content. Pages that load images or scripts from http:// addresses are still mixed content after the redirect.
  • Certificates must cover every redirected hostname. A redirect from www to the bare domain over HTTPS still needs a valid certificate on the www address, otherwise visitors see a warning before the redirect even happens.
  • Certificates expire. If a certificate lapses, visitors see a full-page warning instead of your redirect working. See how to monitor SSL expiry automatically.
  • Keep the path and query. A rule that sends everything to the home page loses deep links and hurts both visitors and search engines.
  • Do not redirect health checks or ACME challenges blindly. Some certificate issuance methods, described in Let's Encrypt's overview, request a file over HTTP on port 80. Check how your issuance method works before forcing every request elsewhere, and keep the verification path reachable if your method needs it.
  • Caches. Browsers and CDNs remember permanent redirects. Plan changes, and avoid pointing a permanent redirect at the wrong target.

Common mistakes seen in real setups

Three situations come up again and again. In the first, a site uses a plugin that forces HTTPS and the administrator also switches on a "force HTTPS" option in the hosting panel. The result is that some pages hop twice, and with a CDN in front they sometimes loop. The fix is to pick one mechanism, switch the other off and test again. In the second, the rule sends every HTTP request to the HTTPS home page, so old links that point to deep articles drop visitors on the front page. The fix is to use a variable that keeps the path and query, such as $request_uri in Nginx or %{REQUEST_URI} in Apache. In the third, the administrator redirects www to the bare domain, but the certificate only covers the bare domain, so visitors who type www see a certificate warning before the redirect can even happen. The fix is to obtain a certificate that covers both names.

What these have in common is that none of them shows up when you open the home page once. They appear when you try several spellings of the address. A short test table covering the four combinations of http and https with www and non-www, plus one deep link and one query string, is well worth keeping, and you should repeat it whenever you change hosting, CDN or theme. A few minutes spent on that table is cheaper than a week of confused support messages from visitors who landed on the wrong page.

Summary

A certificate makes HTTPS possible, and a redirect makes it the default. Use a permanent status code, 301 for general pages or 308 where the request method must be kept, and send every http, https, www and non-www combination to one canonical HTTPS address in a single hop. Configure the rule in one place, whether that is the web server, the control panel, the CMS or the CDN. Watch for loops when TLS ends at a proxy, preserve the path and query string, and make sure every redirected hostname has a valid certificate. Test with headers rather than guessing, and then clean up any remaining mixed content.

When you are ready to set up the certificate that your redirect will point to, the SSL certificates page explains the options.

Frequently asked questions

Why do I need a redirect if I already have an SSL certificate?

A certificate only makes HTTPS available. Visitors who type the address without https, follow old links or use old bookmarks still arrive over plain HTTP unless the server sends them to the HTTPS version.

Should I use a 301 or a 308 redirect?

Both are permanent. A 301 is the long-established choice and works everywhere, but some clients change a POST into a GET when following it. A 308 keeps the original method. For ordinary page visits a 301 is fine; for forms or APIs consider 308.

What causes a redirect loop?

A loop happens when the server keeps sending visitors back to HTTPS because it cannot tell the original request was already secure. This is common behind a CDN, load balancer or reverse proxy that terminates TLS and talks to your server over HTTP.

Do I need to redirect the www and non-www versions too?

Yes. Choose one canonical hostname and send every other combination of http, https, www and non-www to it in a single hop, so visitors and search engines see one consistent address.

How do I test that the redirect works?

Request the HTTP address with a command-line tool or an online checker and read the status code and Location header. You want one hop to the final HTTPS address, with a permanent status code and no loop.

About the author

Bob Kanokphon Thongdee, founder of XPASS VPN

Bob Kanokphon Thongdee

  • Founder of XPASS VPN
  • Co-founder of eSIMonline.asia
  • More than 7 years in online marketing
  • Founder of Cojourney Visa and Cojourney Travel

Need an SSL certificate?

Pick the certificate type that fits your site and see the price before you check out.

SSL from $14.99

See SSL certificates

Keep reading

SSL Guides

What Is Mixed Content and How Do You Fix It?

A padlock that disappears or a script that never loads is often mixed content. Learn what it is, how to find the offending URLs and how to fix them properly.

10 min read

SSL Guides

How to Monitor SSL Certificate Expiry Automatically

Expired certificates still catch careful teams out. Learn how to read the expiry date, automate the check, route alerts and build a renewal routine that works.

10 min read

SSL Guides

How to Fix an SSL Certificate Name Mismatch

A certificate name mismatch means the hostname in the address bar is not on the certificate. Here is how to find the cause and put the right names on the right server.

10 min read