How to Force SSL (HTTPS) Using .htaccess

  • Saturday, 21st March, 2026
  • 15:09pm

In today’s web environment, security is no longer optional. Enforcing HTTPS ensures that all data transferred between your users and your website is encrypted, improving trust, SEO rankings, and overall security. One of the simplest ways to force HTTPS on an Apache server is by using the .htaccess file.

In this guide, we’ll walk through how to force SSL using .htaccess, explain how it works, and cover common issues you might encounter.


Why Force HTTPS?

Before diving into the setup, here are a few reasons why HTTPS is essential:

  • Security: Encrypts data between the browser and server

  • SEO Boost: Search engines like Google prioritize HTTPS sites

  • User Trust: Browsers label HTTP sites as “Not Secure”

  • Compliance: Required for handling sensitive data (e.g., payments, logins)


Prerequisites

Before forcing HTTPS, make sure:

  1. You have an SSL certificate installed on your server

  2. Your website is accessible via https://yourdomain.com

  3. Apache’s mod_rewrite module is enabled


Basic .htaccess Redirect to Force HTTPS

To redirect all HTTP traffic to HTTPS, add the following code to your .htaccess file (usually in your root directory):

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

How This Works

  • RewriteEngine On
    Enables Apache’s rewrite engine

  • RewriteCond %{HTTPS} off
    Checks if the connection is not HTTPS

  • RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
    Redirects all requests to the HTTPS version

  • [L,R=301]

    • L = Last rule (stop processing further rules)

    • R=301 = Permanent redirect (good for SEO)


Force HTTPS for a Specific Domain (With www)

If you want to enforce both HTTPS and www, use:

RewriteEngine On

# Force HTTPS and www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Force HTTPS Without www

If you prefer non-www URLs:

RewriteEngine On

# Remove www and force HTTPS
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [L,R=301]

Handling Reverse Proxies (e.g., Cloudflare)

If you’re using a service like Cloudflare, your server might not detect HTTPS correctly. Use this instead:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Common Issues & Fixes

1. Redirect Loop (Too Many Redirects)

  • Cause: SSL already forced elsewhere (e.g., hosting panel or CDN)

  • Fix: Remove duplicate redirect rules or adjust conditions

2. Mixed Content Warning

  • Cause: HTTP resources (images, scripts) on an HTTPS page

  • Fix: Update all URLs to use https:// or protocol-relative URLs

3. SSL Not Working

  • Cause: Certificate not installed correctly

  • Fix: Verify SSL via your hosting panel or provider


Best Practices

  • Always use 301 redirects for

« Back