最近帮朋友申请ssl升级网站到https,需要将原有的http 301重定向到https,这牵扯到将http://www.domain.com、http://domain.com以及https://domain.com统一跳转到https://www.domain.com,也就是把域名从www和不带www的http,以及https不带www的都跳转到带www的https。可以在web.config文件内的<system.webServer>节点内添加如下配置即可。
<system.webServer>
<rewrite>
<rules>
<!--http://www跳转到https://www-->
<rule name="WWW HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="^www" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
<!--http://跳转到https://www-->
<rule name="None WWW HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="^[^www]" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}/{R:1}" />
</rule>
<!--https://跳转到https://www-->
<rule name="None WWW HTTPS to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="^[^www]" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
- <system.webServer>
- <rewrite>
- <rules>
- <!--http://www跳转到https://www-->
- <rule name="WWW HTTP to HTTPS" stopProcessing="true">
- <match url="(.*)" />
- <conditions>
- <add input="{HTTPS}" pattern="off" ignoreCase="true" />
- <add input="{HTTP_HOST}" pattern="^www" ignoreCase="true" />
- </conditions>
- <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
- </rule>
- <!--http://跳转到https://www-->
- <rule name="None WWW HTTP to HTTPS" stopProcessing="true">
- <match url="(.*)" />
- <conditions>
- <add input="{HTTPS}" pattern="off" ignoreCase="true" />
- <add input="{HTTP_HOST}" pattern="^[^www]" ignoreCase="true" />
- </conditions>
- <action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}/{R:1}" />
- </rule>
- <!--https://跳转到https://www-->
- <rule name="None WWW HTTPS to HTTPS" stopProcessing="true">
- <match url="(.*)" />
- <conditions>
- <add input="{HTTPS}" pattern="on" ignoreCase="true" />
- <add input="{HTTP_HOST}" pattern="^[^www]" ignoreCase="true" />
- </conditions>
- <action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}/{R:1}" />
- </rule>
- </rules>
- </rewrite>
- </system.webServer>
<system.webServer>
<rewrite>
<rules>
<!--http://www跳转到https://www-->
<rule name="WWW HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="^www" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
<!--http://跳转到https://www-->
<rule name="None WWW HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="^[^www]" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}/{R:1}" />
</rule>
<!--https://跳转到https://www-->
<rule name="None WWW HTTPS to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="^[^www]" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
另外,如果你的iis没有安装重定向模块,则需首先添加url重定向模块,否则上面的配置无法生效。