Certificates

Redirect HTTP to HTTPS: Apache, Nginx, Cloudflare

In short. Your website still answers on the old "http://" address: visitors who use it see "Not secure" in the address bar and hesitate to fill in a form or pay. All it takes is an automatic redirect to the secure "https://" version, set up once on the server or at your hosting provider. Your IT provider or host can do it, sometimes just by ticking a "Force HTTPS" box. Expect a few minutes to an hour.

To redirect HTTP to HTTPS, the server must answer every http:// address with a permanent redirect (301) to the same address in https://, path included. On Apache, the official documentation recommends a Redirect directive in the port 80 virtual host (or mod_rewrite in an .htaccess). On Nginx, a return 301 directive. At Cloudflare, the Always Use HTTPS option. A single redirect is enough: adding them in several places creates loops.

Last checked: November 18, 2026, in the Apache, Nginx and Cloudflare documentation.

Before you start

The site must already work in https:// with a valid certificate. Open https://your-domain.com: if it shows a warning, fix the certificate first (see "Your connection is not private").

Step 1: choose a single place for the redirect

Your caseWhere to redirect
Site behind CloudflareCloudflare (Always Use HTTPS), and not on the server
Shared hosting"Force HTTPS" checkbox in the customer area, otherwise .htaccess
Apache server you managePort 80 virtual host
Nginx server you managePort 80 server block

Step 2: set up the redirect

<VirtualHost *:80>
    ServerName www.your-domain.com
    Redirect permanent "/" "https://www.your-domain.com/"
</VirtualHost>

Redirect keeps the path: http://www.your-domain.com/contact leads to https://www.your-domain.com/contact. The Apache wiki advises starting without permanent (a temporary redirect), then switching to permanent once everything works.

Apache, .htaccess file (no access to the configuration)

RewriteEngine On
RewriteCond "%{HTTPS}" !=on
RewriteRule "^(.*)" "https://%{SERVER_NAME}$1" [R=301,L]

The Apache documentation presents this version as the alternative when you have no access to the main configuration.

Nginx

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

The return directive accepts a redirect code (301, 302, 303, 307 or 308) followed by an address, which can contain variables.

Cloudflare

  1. In SSL/TLS > Overview, check that the encryption mode is not set to Off: otherwise the option is hidden.
  2. In Edge Certificates, turn on Always Use HTTPS.

Cloudflare recommends not also redirecting on the origin server: that is the classic cause of redirect loops.

WordPress

The redirect is done on the server (above). In WordPress, then replace the two site addresses with their https:// version (Settings > General), so pages stop generating links in http://.

Step 3: check

curl -sI http://your-domain.com/contact

You should get 301 and a Location: https://…/contact line, with the same path. Also test http://www.your-domain.com and every public subdomain.

Then add the HSTS header so the browser goes straight to HTTPS: see Enable HSTS.

Check your domain for free. AttackScout checks the redirect to HTTPS, your certificate and the HSTS header from the Internet, in one minute, no account. Check my domain

The pitfalls

  • Redirect loop ("too many redirects"): the redirect is done twice, for example at Cloudflare and on the server. Keep only one.
  • Redirect to the home page: http://…/contact must lead to https://…/contact, not to https://…/. Otherwise old links and the pages' search ranking are lost.
  • Mixed content: the page is in HTTPS, but loads images or scripts in http://. Cloudflare points this out: forcing HTTPS does not fix it. Replace those links with https:// or relative links.

Frequently asked questions

301 or 302?

301, once the redirect has been tested. It is permanent: search engines update their index, as the Apache documentation points out.

Does the redirect cost search ranking?

A page-to-page 301 redirect, which keeps the paths, lets search engines carry each page over to its new address.

My site still shows "Not secure" after the redirect.

Check for mixed content and the certificate. See site "Not secure".

Sources

Accessed on November 18, 2026.