Tutorial · dns-ssl · Published 2026-08-15 · 4 min read

How to redirect HTTP to HTTPS

How to redirect HTTP to HTTPS with a 301, covering .htaccess, Nginx, WordPress and Cloudflare, plus redirect loop troubleshooting.

Flat editorial illustration showing a globe of orbiting nodes resolving into a key and shield form, dotted resolution paths converging.
Illustration: this article at a glance.

Overview

An HTTP to HTTPS redirect makes every old http:// URL permanently become its https:// twin with a single 301 response. It protects the search value and the booking/bookmark integrity that the previous HTTP URLs earned. The trick is to have exactly one redirect at the edge, never a chain, and never a redirect from https back to http, which is the classic loop.

Editorial close-up illustration showing a globe of orbiting nodes resolving into a key and shield form, dotted resolution paths converging.
Illustration: a closer look at the technique described above.

The universal rule: one hop

| What | Response code | Where |

| Client visits http://example.com/page | 301 Moved Permanently with Location: https://example.com/page | At the edge (CDN/proxy) or web server |

Only the same host: http://example.com should redirect to https://example.com, not to https://www.something-else.com. If the domain also needs a bare-to-www move, do it in the same edge rule so it stays one suggestion.

1. Nginx

In the server block for port 80:

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

The main server block listens on 443. Verify with nginx -t and reload.

2. Apache (.htaccess or vhost)

In the virtual host or .htaccess in the docroot:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

Works on shared hosting where only .htaccess is allowed; on Apache config, put it in the port 80 <VirtualHost>.

3. WordPress

4. Cloudflare and other CDNs

If the site runs behind Cloudflare, use the SSL/TLS > Edge Certificates "Always Use HTTPS" toggle before adding a server redirect. Edge-level redirect rules replace http:// with https:// at the proxy, so the origin never sees HTTP at all, which also protects mixed-content upgrades.

The redirect loop trap

A loop happens when the origin requests https but the proxy reverses it, or two software layers point at each other. Symptoms: ERR_TOO_MANY_REDIRECTS in the browser, curl showing endless 301s. The standard cause chain is Cloudflare "Flexible" mode forcing origin http while the origin HTTPS redirect forces https again. Fix: set a proper Full (strict) mode when origin has a valid certificate, or if Flexible stays, drop the origin-level force.

Verifying the redirect chain

Sending an HSTS header once the move is clean

Once http reliably redirects to https and every subresource loads over https, add a Strict-Transport-Security header on the https response, for example Strict-Transport-Security: max-age=31536000. Browsers that have seen that response then refuse to plain-http requests to the domain from then on. Do not enable HSTS before the redirect and all subresources are clean, or repeat visitors can be locked out of the site during the transition.

Fast but safe edge cases

The 301 article explains the permanent status in detail; the mixed-content fix article prevents the padlock problems that come after the move.

Need a website built, fixed, optimised, migrated or replaced?

This technical resource is written by CSMBAC, a small design and development studio. If you would rather hand the problem to a professional, the website service page explains how we build enquiry-ready websites.

Explore website services