htaccess redirect to https://www

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

    To redirect http:// or https:// to https://www you can use the following rule on all versions of apache :

    RewriteEngine on
    
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
    

    Apache 2.4

    RewriteEngine on
    
    RewriteCond %{REQUEST_SCHEME} http [OR]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
    

    Note that The %{REQUEST_SCHEME} variable is available for use since apache 2.4 .

    0 讨论(0)
  • If you are using CloudFlare or a similar CDN you will get an infinite loop error with the %{HTTPS} solutions provided here. If you're a CloudFlare user you'll need to use this:

    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    0 讨论(0)
提交回复
热议问题