Forcing SSL and WWW using .htaccess

前端 未结 7 806
野性不改
野性不改 2020-12-13 00:59

I would like to know if this code in .htaccess for forcing SSL and WWW in URL is correct, because with another codes I usually get redirect loop, e.g. Rew

7条回答
  •  天涯浪人
    2020-12-13 01:07

    I found a mod_rewrite solution that works well for both proxied and unproxied servers.

    If you are using CloudFlare, AWS Elastic Load Balancing, Heroku, OpenShift or any other Cloud/PaaS solution and you are experiencing redirect loops with normal HTTPS redirects, try the following snippet instead.

    RewriteEngine On
    
    # If we receive a forwarded http request from a proxy...
    RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
    
    # ...or just a plain old http request directly from the client
    RewriteCond %{HTTP:X-Forwarded-Proto} =""
    RewriteCond %{HTTPS} !=on
    
    # Redirect to https version
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

提交回复
热议问题