國內高防部署證書後自動301跳轉https訪問

發佈時間:2021-05-20

注意:

1、規則裡面的域名替換為實際域名

2、服務器內不需要開啓部署HTTPS

3、寶塔面板不要開啓強制HTTPS

apache環境:

在網站根目錄下創建一個.htaccess文件,然後寫入以下規則,如果文件本身就存在,在裡面添加以下規則

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:From-Https} !^on$ [NC]                                     
RewriteCond %{HTTP_HOST} ^(www.)?abc.com$ [NC]                # 將abc.com和www.abc.com跳轉到https://www.abc.com,防止apache子站繼承上級目錄.htaccess受影響
RewriteRule ^(.*)$ https://www.abc.com/$1 [R=301,L]
</IfModule>

Nginx環境:

編輯對應站點配置文件,在server配置節點中添加判斷if這段配置

server
{
listen 80;
server_name abc.com;   
}

#在網站server配置節中添加下面代碼
if ( $http_from_https != 'on' ){
     rewrite ^(.*) https://www.abc.com$1 permanent;           # abc.com對應修改為您自已的域名
 }

Windows系統 IIS7及以上版本:

在網站根目錄下創建web.config文件,寫入一下代碼。如果web.config本身就存在,在<system.webServer>配置節點後添加<rewrite>.......</rewrite> 這一段規則內容

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
        <rewrite>
            <rules>
               <rule name="301" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">                       
                         <add input="{HTTP_FROM_HTTPS}" pattern="^on$" negate="true" />  
                  </conditions>
                    <action type="Redirect" url="https://www.abc.com/{R:1}" redirectType="Permanent" />
               </rule>
            </rules>
        </rewrite>
    </system.webServer> 
</configuration>

Windows2003系統 IIS6環境:

iis6的環境需要加載rewrite.dll組件,在httpd.ini或者httpd.conf裡面添加一下規則

RewriteEngine On
RewriteCond %{HTTP:From-Https} !^on$ [NC]                               
RewriteRule ^(.*)$ https://www.abc.com/$1 [R=301,L]       # www.abc.com對應修改為您自已的域名
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ https://www.abc.com$1 [R=301,L]        # www.abc.com對應修改為您自已的域名