Force HTTPS htaccess rewrite suggestions to minimize redirects


#1

Hello. I added the rewrite snippet below at the top of my .htaccess file to force HTTP to HTTPS (no WWW).

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

It went fine, but I am getting the redirect chain below.

2 Redirect(s)
 
http://www.mydomain.com
301 redirect to
https://www.mydomain.com/

https://www.mydomain.com/
301 redirect to
https://mydomain.com/

Final status code: 200

I would like to make it just 1 redirect if possible. Is there anything I failed to add to my rewrite snippet above to make it just so?


#2

Try the following

To force https and non-www

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

To force https and www

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

#3

Thank you. I used the first one you provided (To force https and non-www). However after using it, when I entered my domain in the redirect mapping tool https://varvy.com/tools/redirects/ it now couldn’t seem to figure out the how redirects are mapped. It doesn’t show if it is being redirected once or twice. Would you have any ideas why?