.htaccess too many redirects when forcing HTTPS

by imlost   Last Updated August 17, 2018 18:04 PM - source

When I use these lines in my .htaccess file, everything works well:

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

But how can I make it work without manually writing the domain in the URL?

I tried these lines:

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

But then I get the "Too many redirects" errors : enter image description here

I don't understand why, I just replaced the domain with {HTTP_HOST}. When I do that, it redirects from http://www.example.com to http://example.com (since it is my main domain) then http://example.com to https://example.com

If I add this line : RewriteCond %{HTTP_HOST} !^www\. [NC] it does not change anything.

Related Questions

Force https and no www using htaccess

Updated October 12, 2016 09:01 AM

.htaccess redirects only the homepage to https

Updated August 17, 2018 12:04 PM