Dev Notes

How to Force HTTPS with .htaccess (Apache, LiteSpeed, WordPress, Cloudflare)

Force HTTPS with .htaccess: 301 redirects, www rules, Cloudflare and HSTS
Dev Notes November 24, 2026 Tamir Khan No Comments

Quick answer

To force HTTPS on an Apache or LiteSpeed server, add a 301 rule at the top of your .htaccess: RewriteEngine On, RewriteCond %{HTTPS} off, RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]. Install a valid SSL certificate first, combine the HTTPS and www rules into a single redirect, and use X-Forwarded-Proto instead of %{HTTPS} if your site sits behind Cloudflare or a load balancer.

Key takeaways

  • Install and test the SSL certificate before forcing HTTPS, or visitors will see errors.
  • Use a 301 (permanent) redirect so Google transfers rankings to the HTTPS URLs.
  • Redirect in one hop: http://www to https://non-www, not through two redirects.
  • Behind a proxy or CDN, check X-Forwarded-Proto to avoid redirect loops.
  • Add HSTS only after everything works on HTTPS.

Once your SSL certificate is installed, every visitor and every search engine should land on the HTTPS version of your site. A few lines in .htaccess do this on Apache and LiteSpeed servers, which covers most shared and WordPress hosting. If you’re not sure what SSL is or which certificate you need, start with What is SSL/TLS?

Before you start

  • Open https://yourdomain.com and check the padlock appears without warnings.
  • Download a copy of your current .htaccess file (in the site’s root, often public_html). It may be hidden; enable “show hidden files” in your file manager.
  • Know whether you use Cloudflare, another CDN or a load balancer in front of your server.

What is the basic .htaccess rule to force HTTPS?

Add this at the very top of .htaccess, above any WordPress or other rules:

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

It checks whether the request arrived over plain HTTP and sends a permanent redirect to the same address on HTTPS, keeping the path and query string.

How do I force HTTPS and non-www in one redirect?

Pick one canonical version of your domain. This sends everything to https://example.com in a single hop:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

How do I force HTTPS and www?

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]

Need a website that turns visitors into enquiries?

My team builds fast, secure, SEO-ready websites and web apps for growing businesses.

Get a website quote WhatsApp me

How do I avoid redirect loops behind Cloudflare or a load balancer?

When a CDN or load balancer handles SSL and talks to your server over HTTP, %{HTTPS} is always “off”, so the rule redirects forever (“ERR_TOO_MANY_REDIRECTS”). Check the forwarded protocol instead:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

With Cloudflare, also set SSL/TLS mode to Full (strict) with a valid certificate on your server, and turn on “Always Use HTTPS” in Cloudflare. You can then often remove the server rule entirely.

How do I force HTTPS in WordPress?

  1. Go to Settings → General and set both WordPress Address and Site Address to https://.
  2. Add the redirect rule above # BEGIN WordPress in .htaccess. Never edit inside the WordPress block, because it is overwritten.
  3. Fix old http:// links in content with WP-CLI: wp search-replace 'http://example.com' 'https://example.com' --skip-columns=guid.
  4. Clear your cache plugin and CDN cache.

How do I test the redirect?

curl -I http://example.com/some-page/?a=1
curl -I http://www.example.com/

Each should return HTTP/1.1 301 with a Location: header pointing straight to the final HTTPS URL. Then check in a private browser window and run an SSL test for mixed content warnings.

Should I add HSTS?

HTTP Strict Transport Security tells browsers to always use HTTPS for your domain. Add it once everything works:

<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>

Start with a short max-age (for example 300) to test, and only add includeSubDomains if every subdomain supports HTTPS. Browsers remember HSTS, so mistakes are hard to undo quickly.

What if I use Nginx?

Nginx ignores .htaccess. Add a server block instead:

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

Frequently asked questions

Does redirecting to HTTPS affect SEO?

A correct 301 redirect from HTTP to HTTPS keeps your rankings, and HTTPS is a light ranking signal. Update your sitemap, internal links and Search Console property to the HTTPS version.

Why do I get too many redirects after forcing HTTPS?

Usually a CDN or load balancer terminates SSL and sends HTTP to your server, or two rules (server and CDN or plugin) fight each other. Check X-Forwarded-Proto and keep only one redirect source.

Should I use a 301 or 302 redirect for HTTPS?

Use 301. It is permanent and tells search engines to index the HTTPS URLs. A 302 is temporary.

Where is the .htaccess file?

In the root folder of your website, usually public_html or www. Files starting with a dot are hidden, so enable hidden files in your FTP client or file manager.

Tamir Khan: business growth and digital strategy consultant in Mumbai. Since 2011 he has built websites, SEO, Google Ads, cloud and automation systems for businesses in India, the UK, Canada, the USA and the UAE. Strategy comes from Tamir; delivery is by the NexGeno team. About Tamir · LinkedIn

Last updated: 25 November 2026

Prev No Next Post Available

Related Posts

Quick Links

Newsletter

I am the Founder and Chief Technology Officer at NexGeno Technology Pvt Ltd. With 12+ years of experience, I specialize in digital strategy, business automation, AI integration, web development, and marketing.I've worked with startups, SMEs, and global clients, offering tailored strategies that deliver measurable results.

© 2025 Tamir Khan - All Rights Reserved | Managed by NexGeno