htaccess redirect to https://www

前端 未结 14 2017
夕颜
夕颜 2020-11-21 11:01

I have the following htaccess code:



RewriteEngine On
RewriteCond !{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{RE         


        
14条回答
  •  臣服心动
    2020-11-21 11:53

    BAD SOLUTION AND WHY!

    Don't ever use the solution below because when you are using their code that is something like:

    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
    

    The browser goes to:

    http://example.com
    

    Then redirects to:

    https://example.com
    

    Then redirects to:

    https://www.example.com
    

    This is too much request to the server.

    Most of the answers even accepted one has this problem.


    BEST SOLUTION AND THE ANSWER

    This code has an [OR] condition to prevent dual changes at url!

    RewriteEngine on
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]
    

提交回复
热议问题