Force SSL HTTPS for subdomain with .htaccess

前端 未结 2 1581
南方客
南方客 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.

提交回复
热议问题