htaccess redirect to https://www

前端 未结 14 2037
夕颜
夕颜 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:49

    To first force HTTPS, you must check the correct environment variable %{HTTPS} off, but your rule above then prepends the www. Since you have a second rule to enforce www., don't use it in the first rule.

    RewriteEngine On
    RewriteCond %{HTTPS} off
    # First rewrite to HTTPS:
    # Don't put www. here. If it is already there it will be included, if not
    # the subsequent rule will catch it.
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # Now, rewrite any request to the wrong domain to use www.
    # [NC] is a case-insensitive match
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    About proxying

    When behind some forms of proxying, whereby the client is connecting via HTTPS to a proxy, load balancer, Passenger application, etc., the %{HTTPS} variable may never be on and cause a rewrite loop. This is because your application is actually receiving plain HTTP traffic even though the client and the proxy/load balancer are using HTTPS. In these cases, check the X-Forwarded-Proto header instead of the %{HTTPS} variable. This answer shows the appropriate process

    0 讨论(0)
  • 2020-11-21 11:49

    If you are on CloudFlare, make sure you use something like this.

    # BEGIN SSL Redirect
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>
    # END SSL Redirect
    

    This will save you from the redirect loop and will redirect your site to SSL safely.

    P.S. It is a good idea to if check the mod_rewrite.c!

    0 讨论(0)
  • 2020-11-21 11:52

    Set in your .htaccess file

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    0 讨论(0)
  • 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]
    
    0 讨论(0)
  • 2020-11-21 11:55

    Similar to Amir Forsati's solution htaccess redirect to https://www but for variable domain name, I suggest:

    RewriteEngine on
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$ [NC]
    RewriteRule ^ https://www.%2%{REQUEST_URI} [R=301,L]
    
    0 讨论(0)
  • 2020-11-21 11:56

    This is the best way I found for Proxy and not proxy users

    RewriteEngine On
    
    ### START WWW & HTTPS
    
    # ensure www.
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # ensure https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    ### END WWW & HTTPS
    
    0 讨论(0)
提交回复
热议问题