Migrations
Force HTTPS and www in .htaccess in One Hop (Plus the Cloudflare Loop Fix)
The usual force HTTPS htaccess snippet works, and that's the problem: it works on its own, then someone adds a second snippet for www, and now http://example.com/page takes two redirects to arrive. One to switch to HTTPS, one to add www. Multiply that by every old link pointing at your site.
The fix is a single rule that handles both at once, so every version of every URL lands on the final address in one hop. Below is that rule for www and non-www sites, the Nginx and Cloudflare versions, and the fix for the "too many redirects" loop that Cloudflare's Flexible SSL mode causes.
If you're switching to HTTPS as part of a bigger move, it's one step in a site migration done with a tested redirect map, and you can send me the details here. Otherwise, back up your .htaccess and read on.
Why two rules make two hops
Here's what the common two-snippet setup does to one old link:
http://example.com/services/
-> 301 -> https://example.com/services/ (HTTPS rule)
-> 301 -> https://www.example.com/services/ (www rule)
-> 200Googlebot follows it, so it's not a disaster. But Google's site-move guide advises redirecting to the final destination directly, because each hop adds delay for visitors and some browsers handle long chains badly. And chains grow. Add a page redirect on top and that old link is now three hops deep.
Force HTTPS in htaccess plus www, in one rule
Pick the version that matches the address you want people to see. Replace example.com with your domain. These go at the top of the .htaccess file in your site root, above # BEGIN WordPress.
If your site uses www
# Sends http://, http://www. and https:// (no www) straight to https://www. in ONE hop.
# Could break: other hostnames served from this folder (addon domains, staging
# subdomains). They'll be redirected too, so list them in their own conditions first.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L,NE]If your site uses no www
# Sends http://, http://www. and https://www. straight to https://example.com in ONE hop.
# Could break: same note as above about other hostnames in this folder.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L,NE]How it reads, in plain English: "If the connection isn't HTTPS, or the hostname isn't exactly the one I want, send the visitor to the right hostname on HTTPS with the same path." The [OR] is what merges two redirects into one.
A few details that matter:
- The domain is typed out, not pulled from
%{HTTP_HOST}. Building the target from the incoming hostname is how you end up redirectinghttp://example.comtohttps://example.comand then needing a second rule forwww. R=301makes it permanent. Without a number, mod_rewrite defaults to a 302, which is temporary.NEstops Apache from re-encoding characters like%20that are already encoded in the old URL.- The query string comes along on its own. A link with
?utm_source=newsletterkeeps its tracking tags.
Match WordPress's own setting
WordPress has its own idea of your address, in Settings, then General: "WordPress Address" and "Site Address". If those say http://example.com while your rule says https://www.example.com, WordPress redirects back and you get a loop or an extra hop. Set both fields to exactly the address in your rule.
The Cloudflare "too many redirects" loop
This is the one that sends people to forums at midnight. You add the HTTPS rule, turn on Cloudflare, and the browser says "too many redirects."
Here's why. In Cloudflare's Flexible SSL mode, visitors connect to Cloudflare over HTTPS, but Cloudflare connects to your server over plain HTTP. Your server sees %{HTTPS} off on every request, redirects to HTTPS, Cloudflare fetches it over HTTP again, and round it goes. Cloudflare's own troubleshooting page describes exactly this: loops occur in Flexible mode if your origin redirects HTTP to HTTPS.
Apache's docs flag the same problem for any proxy: behind a load balancer or SSL terminator, %{HTTPS} will always report off, even for visitors who connected securely.
You have three ways out. In order of preference:
Option A: switch to Full (strict)
Put an SSL certificate on your server (most hosts include a free one) and set Cloudflare's SSL/TLS mode to Full (strict). Now Cloudflare talks to your server over HTTPS, %{HTTPS} reports on, and the one-hop rule above works unchanged. This is the fix I use almost every time. It's also the only option that encrypts the leg between Cloudflare and your server.
Option B: do the redirect at Cloudflare instead
Cloudflare itself recommends not performing redirects at your origin when it's in front. Remove the rule from .htaccess and create two redirect rules using wildcard patterns, with "Preserve query string" on:
# Rule 1: any scheme on the bare domain -> https://www. (the first * is "s" or nothing)
Request URL: http*://example.com/*
Target URL: https://www.example.com/${2}
Status code: 301
# Rule 2: plain http on www -> https://www.
Request URL: http://www.example.com/*
Target URL: https://www.example.com/${1}
Status code: 301One catch. Cloudflare's "Always Use HTTPS" switch can fire before your rule, which sneaks the second hop back in for http://example.com. Test with curl (below). If you still see two hops, turn "Always Use HTTPS" off and let these two rules do the job.
Option C: read Cloudflare's header (last resort)
If you're stuck on Flexible for now, tell Apache to check the header Cloudflare sends. Cloudflare sets X-Forwarded-Proto to the protocol the visitor used:
# For Cloudflare Flexible mode only. Uses Cloudflare's header instead of %{HTTPS}.
# Could break: direct visits that bypass Cloudflare won't be forced to HTTPS.
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L,NE]Apache warns to trust that header only when the proxy overwrites it on every request, since anyone hitting your server directly can fake it. For a redirect that's low risk, but it's another reason to move to Full (strict) when you can.
Don't block your certificate renewals
Free certificates from Let's Encrypt often renew by fetching a file over plain HTTP from /.well-known/acme-challenge/. A strict HTTPS redirect usually doesn't stop that, but some setups choke on it. The same Apache recipe page shows the exception to put before your HTTPS rule:
# Lets certificate validation requests through over plain HTTP.
# Could break: nothing. It only matches the ACME challenge folder.
RewriteRule ^\.well-known/acme-challenge/ - [L]The Nginx version
Nginx doesn't read .htaccess. The cleanest one-hop setup uses separate server blocks with return, which stops processing at once:
# Every http request, www or not, goes straight to https://www. in one hop.
server {
listen 80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
# HTTPS on the bare domain goes to www. (certificate lines omitted)
server {
listen 443 ssl;
server_name example.com;
return 301 https://www.example.com$request_uri;
}$request_uri carries the path and query string across untouched.
Test all four doors
There are four ways into any site. Test each one and count the hops:
# Each line should show exactly one 301, then 200 on the final URL
# (the final address itself should show 0 hops).
for u in http://example.com/ http://www.example.com/ https://example.com/ https://www.example.com/; do
curl -s -o /dev/null -L -w "$u -> %{num_redirects} hops -> %{url_effective}\n" "$u"
doneRepeat with a deep URL like /services/roof-repair/?utm_source=test to confirm the path and query string survive.
Final Thoughts
Merge your HTTPS and www rules into one with [OR], type your domain into the target, and keep WordPress's address settings matched. On Cloudflare, use Full (strict) and the loop disappears. Then test all four versions of your homepage plus a deep link.
An HTTPS switch on the same domain doesn't need Google's Change of Address tool, by the way. Google detects it on its own. Here's what that tool is actually for. And if a typo in .htaccess has taken the site down, here's how to fix a 500 error after an edit. The full htaccess redirect cheat sheet covers page and folder rules.
If you'd like this done and tested for you, tell me about your setup. A host-to-host move with HTTPS set up properly starts at $300.
Force HTTPS htaccess FAQs
How do I redirect HTTP to HTTPS in .htaccess?
Add RewriteEngine On, then RewriteCond %{HTTPS} off, then RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L] at the top of your .htaccess. To handle www in the same hop, add a second condition for the hostname with [OR] between them, as shown above. Always use R=301, because a bare R sends a temporary 302.
Why do I get "too many redirects" after forcing HTTPS on Cloudflare?
Usually because Cloudflare's SSL mode is Flexible. Cloudflare reaches your server over plain HTTP, your server redirects to HTTPS, and the loop repeats. Switch the SSL/TLS mode to Full (strict) with a certificate on your server, or move the redirect to Cloudflare and remove it from .htaccess.
Should my site use www or not?
It doesn't matter for SEO. What matters is picking one and redirecting every other version to it in one hop. Use whichever is already in your Search Console data, backlinks and printed materials, because switching later creates redirects for no gain.
Does forcing HTTPS hurt SEO?
No. Moving to HTTPS with permanent redirects is a standard change, and Google detects it without the Change of Address tool. Expect some short ranking movement while Google recrawls. Problems come from redirect chains, mixed http links inside pages, or an http canonical tag left in your SEO plugin settings.
Want a second pair of eyes on your site?
Tell me what's going on in the website migration questionnaire. I reply with a plan and a fixed quote within one business day.