1. 程式人生 > >IIS7 IIS 7.5的http 做301重定向到https

IIS7 IIS 7.5的http 做301重定向到https

我們的伺服器部分是windows下的IIS平臺,一些使用者安裝了SSL https證書後,不會控制301跳轉,下面直接貼出程式碼,根據需要把程式碼放到web.config檔案裡即可。


http跳轉到https

這個程式碼段是http重定向到https, 注意,要放在<rules>和</rules>之間。

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

http跳轉到https

提醒:裡面的網址記得改成你自己的網站,別懶得只會copy!

這個程式碼段是無3w重定向到有3w域名, 注意,要放在<rules>和</rules>之間。

<rule name="www" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^domain\.com$" />
                    </conditions>
                    <action type="Redirect" url="https://www.domain.com/{R:0}" />
 </rule>

完整的web.config程式碼

下面程式碼包含了,無3w網址,跳轉到有3w網址; 以及, http全部跳轉到https

當然了,知道大家懶,已經將兩份整合到一起,將下面全部程式碼儲存成web.config檔案,存放到空間的web目錄下即可。再次提醒:裡面的網址記得改成你自己的網站,別懶得只會copy!

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="www" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^domain\.com$" />
                    </conditions>
                    <action type="Redirect" url="https://www.domain.com/{R:0}" />
                </rule>
				<rule name="HTTP to HTTPS redirect" stopProcessing="true">
				<match url="(.*)" />
				<conditions>
				<add input="{HTTPS}" pattern="off" ignoreCase="true" />
				</conditions>
				<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
				</rule>
			</rules>
        </rewrite>
    </system.webServer>
</config