IIS URL Rewrite重写规则强制http转https

URL Rewrite模块是IIS(Internet 信息服务管理器)中的一个重要扩展和URL重写工具,通常我们会用来进行301或者302之类的链接跳转,那如何使用URL Rewrite模块将http强制跳转到https链接呢?可以按以下教程操作步骤。

为IIS安装URL Rewrite模块

1、下载url-rewrite(适用于IIS 7, IIS 7.5, IIS 8, IIS 8.5, IIS 10)模块,并安装。

2、安装过程基本上是一路点下一步直至完成。

虽然URL Rewrite2.0已经安装完成,但是在IIS服务器上还是看不到它的图标,这时只要重启IIS就可以了。

web.config配置

可以使用URL Rewrite的重写规则设置向导或者直接贴代码也可以。最简单的方式就是将下方代码,贴到当前网站的 web.config 设置文件相对应的位置中:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
  <rewrite>
   <rules>
     <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
       <match url="(.*)" />
       <conditions>
         <add input="{HTTPS}" pattern="off" />
       </conditions>
       <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
     </rule>
   </rules>
   <outboundRules>
     <rule name="Add the STS header in HTTPS responses">
       <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
       <conditions>
         <add input="{HTTPS}" pattern="on" />
       </conditions>
       <action type="Rewrite" value="max-age=31536000" />
     </rule>
   </outboundRules>
  </rewrite>
</system.webServer>
</configuration>

通过以上两步IIS服务器上的网站http链接就自动跳转到https了。

转载需保留链接来源:VCBeta.CN » IIS URL Rewrite重写规则强制http转https

赞 (0)