Force SSL HTTPS for subdomain with .htaccess

前端 未结 2 1550
南方客
南方客 2021-01-13 07:21

I have this code for forcing SSL on a website generally:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
相关标签:
2条回答
  • 2021-01-13 08:21

    To avoid using the domain or subdomain names I used to use the following code:

      # Redirect HTTP to HTTPS
      RewriteCond %{HTTPS} off
      RewriteCond %{HTTP:X-Forwarded-Proto} !https
      RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    I prefer without WWW.

    %{HTTPS} Set to on if the request is served over SSL.

    %{HTTP_HOST} Everything between the protocol (http:// or https://) and the request (/example-file.txt).

    %{REQUEST_URI} Everything after the server address — for example, a request to http://example.com/my/example-file.txt sets this value as my/example-file.txt.

    %{HTTP:X-Forwarded-Proto} The protocol used when the URL was requested — set to http or https.

    I hope it can be useful for anybody.

    0 讨论(0)
  • 2021-01-13 08:23

    For debugging I use a website I found http://martinmelin.se/rewrite-rule-tester/

    Using the rules you provided

    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    I got an error at

    RewriteCond %{HTTPS} !=on
    

    This might work for you

    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L] 
    

    Found the above example at https://www.sslshopper.com/apache-redirect-http-to-https.html

    0 讨论(0)
提交回复
热议问题