虚拟主机设置http自动跳转到https

如果你的网站以及安装了SSL证书,你就可以将你的网站自动跳转到https以加密网站数据。具体设置方法取决于你的主机类型。

Linux & cPanel

Linux主机使用.htaccess文件控制自动跳转。具体代码如下
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

如果你已经有.htaccess则要注意以下两点:

  • 不要重复复制RewriteEngine On
  • 确保最后两行紧跟RewriteEngine On这一行

Windows & Plesk

Windows主机使用web.config文件控制自动跳转。具体代码如下

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

如果你已经有web.config文件:

  • 请确保你的文件中有包含了以下节组:
    • system.webServer (包含rewrite节)
    • rewrite (包含rules节)
    • rules (包含rule节)

    如果没有的话请添加

  • 在rules节中插入完整的rule节, 包括match, conditions和action节

    注意区分rule和rules