Biggest deals of the year. 30-day money-back guarantee. Limited time deals. 24/7 support. Don't miss out

How to force HTTPS using .htaccess

Published 4 min Read

To force HTTPS using .htaccess, add a 301 redirect that sends HTTP requests to the HTTPS version of the same URL. You can apply the redirect to your entire website, a specific domain, or a specific folder.

Before adding the rule, make sure your SSL certificate is installed and the HTTPS version of the site loads correctly. This tutorial also covers how to verify the redirect, fix common redirect loops, and handle proxy or CDN setups where %{HTTPS} may not detect the original visitor protocol correctly.

Which HTTPS redirect should you use?

Choose the HTTPS redirect based on how much of your website should use HTTPS.

Redirect scopeUse it when
All trafficYou want every HTTP request on the website to redirect to HTTPS. This is the best choice for most websites.
Specific domainMultiple domains point to the same website, but you only want to force HTTPS for one domain.
Specific folderYou only want requests within a particular directory to use HTTPS.

Before adding any redirect, make sure the HTTPS version of your website already loads correctly with a valid SSL certificate. An .htaccess rule redirects visitors to HTTPS, but it does not install or configure the SSL certificate.

If your website is hosted with Hostinger and you only need to redirect the entire site, you can also enable the built-in HTTPS redirect instead of editing .htaccess manually.

How to force HTTPS on all traffic

Force HTTPS on all traffic when you want every HTTP request on the website to redirect permanently to its HTTPS equivalent. This is the best option for most websites using .htaccess.

  1. Open File Manager in your hosting control panel and locate the .htaccess file in your website’s document root, such as public_html. If the file is missing, create or unhide the .htaccess file.
  2. Back up the existing .htaccess file before making changes.
  3. Add the following rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  1. Save the file.

The rule works as follows:

  • RewriteEngine On enables Apache’s rewrite engine.
  • RewriteCond %{HTTPS} off applies the following rule only when the current request is using HTTP rather than HTTPS.
  • RewriteRule redirects the request to the same host and path using https://.
  • R=301 sends a permanent redirect response.
  • L tells Apache to stop processing further rewrite rules for that request once this rule matches.

If RewriteEngine On already exists in the .htaccess file, don’t add a second copy.

How to force HTTPS on a specific domain

Force HTTPS on a specific domain when multiple domains point to the same website but only one should redirect from HTTP to HTTPS.

For example, if yourdomain1.com and yourdomain2.com use the same document root but you only want to force HTTPS for yourdomain1.com, add:

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

Replace yourdomain1.com with the domain you want to redirect.

The first condition limits the rule to that hostname, including its www version. The second condition makes sure the redirect only runs for HTTP requests. Other domains using the same .htaccess file aren’t affected by this rule.

If RewriteEngine On already exists in your .htaccess file, don’t add it again.

How to force HTTPS on a specific folder

Force HTTPS on a specific folder when only one directory and its contents should redirect from HTTP to HTTPS.

  1. Open the folder you want to protect in File Manager.
  2. Open or create the .htaccess file inside that folder.
  3. Add the following rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  1. Save the file.

Because the .htaccess file is inside the target folder, the rule automatically applies to requests for that directory and its subdirectories. You don’t need to include the folder name in RewriteRule.

For example, placing this .htaccess file inside /members/ redirects requests under http://example.com/members/ to HTTPS without applying the rule to unrelated directories.

How to verify the HTTPS redirect

Verify the redirect by opening an HTTP version of a URL covered by your .htaccess rule and confirming that the browser automatically loads the HTTPS version.

  1. Open a private or incognito browser window to avoid a previously cached redirect.
  2. Enter an http:// URL covered by the rule. For example, use your homepage for an all-traffic redirect or a URL inside the target directory for a folder-specific redirect.
  3. Confirm that the browser changes the URL to https:// and loads the page without a certificate warning.

You can also check the HTTP response from the command line:

curl -I http://example.com/

A working permanent redirect should return a 301 response with a Location header pointing to the HTTPS URL.

If the HTTP URL doesn’t redirect, redirects repeatedly, or the HTTPS page shows a certificate error, troubleshoot the configuration before relying on the rule.

How to troubleshoot HTTPS redirect problems

HTTPS redirect problems usually come from conflicting redirects, an SSL configuration issue, or a proxy/CDN setup where Apache can’t detect the visitor’s original protocol correctly.

Fix an HTTPS redirect loop

If the browser shows ERR_TOO_MANY_REDIRECTS, temporarily remove or disable the new .htaccess rule and test the website again.

Then check whether HTTPS is already being forced somewhere else, such as your hosting control panel, CMS, plugin, or CDN. Running multiple redirect methods at the same time can create conflicting rules.

For Hostinger websites, follow the SSL redirect loop troubleshooting steps to check duplicate redirects, Cloudflare settings, and WordPress URLs.

Fix redirect loops behind a proxy or CDN

A reverse proxy, load balancer, or CDN can terminate the HTTPS connection before forwarding the request to your web server. In that setup, Apache may see the connection as HTTP, so %{HTTPS} remains off even when the visitor opened an HTTPS URL.

If your proxy reliably sets and overwrites the X-Forwarded-Proto header, use that header instead of %{HTTPS}:

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

Only use this rule when your hosting provider or proxy documentation confirms that X-Forwarded-Proto is set by a trusted upstream proxy. Otherwise, the header can be spoofed.

If your CDN already provides an HTTPS redirect feature, use one redirect layer instead of forcing HTTPS both at the CDN and origin server.

Fix a redirect that doesn’t work

If an HTTP URL still loads without redirecting, check that:

  • the .htaccess file is in the correct directory
  • mod_rewrite is available and .htaccess overrides are enabled
  • the rule was saved correctly
  • another rewrite rule isn’t taking precedence

If the HTTPS URL itself shows a certificate warning or SSL error, fix the SSL certificate first. An .htaccess redirect only sends visitors to HTTPS; it doesn’t repair an invalid or missing certificate.

Hostinger web hosting banner

What should you do after forcing HTTPS?

After confirming the redirect works, check that your website also loads its images, scripts, stylesheets, and other resources over HTTPS. A page can redirect correctly and still show a Not secure warning if some resources use http:// URLs.

If that happens, fix the insecure resource URLs rather than adding another redirect rule. WordPress users can follow our guide to fixing mixed content errors in WordPress.

If your site uses Hostinger Web or Cloud hosting, HTTPS is normally forced automatically after SSL installation, so manual .htaccess redirects are mainly useful when you need custom redirect behavior.

All of the tutorial content on this website is subject to Hostinger's rigorous editorial standards and values.

Edvinas is a professional mentor and trainer of customer support agents. When he's not teaching the secrets of providing exceptional service, he likes to travel the world and play basketball.

Trusted by 4+ million website owners worldwide

See our 74,491 reviews on Trustpilot

Not sure which plan to choose?

Our smart AI agent, is here to help you find the best package for your project.

See Pricing