Forcing all users to go through SSL with .htaccess

2019-04-16 10:07发布

问题:

I just installed a SSL certificate on my site, but now I'm facing a problem when I try to force all users to go through secure SSL (HTTPS) instead of HTTP. I found that the way to to it is using the code below on .htaccess

#Force SSL on entire site
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

So far, it's not working, I get a redirect loop error.

It's redirecting to the SSL URL, but not working.. without the .htaccess rule it works when i type manually https://example.com

回答1:

I have no idea if this will work for you but I do the same thing for my site and this is the code in my .htaccess file. I am sure I got it from somewhere here on Stack Overflow.

<IFModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IFModule>


回答2:

Try this:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Should work fine...