iis7设置http跳转https实测可用

  前面ytkah和大家聊了Apache设置http如何301到https,现在我们说说iis7设置http跳转https,因为还是有很多人在用iis服务器。首先要先安装url rewrite module,到这里去下载http://www.microsoft.com/en-us/download/details.aspx?id=7435,以administrator管理员的身份去安装这个模块,安装完以后需要重启一下IIS,你就会看到URL Rewrite已经出现了,如下图所示

  第二步我们就可以开始设置跳转了,比如把a.com跳到www.a.com,在a网站的根目录下有一个web.config文件,这个就是设置跳转规则的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
     <system.webServer>
     <rewrite>
         <rules>
             <rule name="WWW Redirect" stopProcessing="true">
               <match url=".*" />
               <conditions>
               <add input="{HTTP_HOST}" pattern="^a.com$" />
               </conditions>
               <action type="Redirect" url="http://www.a.com/{R:0}" redirectType="Permanent" />
             </rule>
         </rules>
     </rewrite>
     </system.webServer>
 </configuration>

  如果要把b.com跳到www.a.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
     <system.webServer>
     <rewrite>
         <rules>
             <rule name="WWW Redirect" stopProcessing="true">
               <match url=".*" />
               <conditions>
               <add input="{HTTP_HOST}" pattern="^b.com$" />
               </conditions>
               <action type="Redirect" url="http://www.a.com/{R:0}" redirectType="Permanent" />
             </rule>
         </rules>
     </rewrite>
     </system.webServer>
 </configuration>

  重点来了,如果有多个站点(包含www.a.com),并且这些站点都要跳转到https://www.a.com,要如何操作呢?首先多站点用|线分割,http跳https需要另外设定一个rule规则,整合代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
     <system.webServer>
     <rewrite>
         <rules>
             <rule name="WWW Redirect" stopProcessing="true">
               <match url=".*" />
               <conditions>
               <add input="{HTTP_HOST}" pattern="^a.com|b.com$" />
               </conditions>
               <action type="Redirect" url="https://www.a.com/{R:0}" redirectType="Permanent" />
             </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://www.a.com" />
             </rule>
 
         </rules>
     </rewrite>
     </system.webServer>
 </configuration>

  以上算比较完美的解决方案了,不明白的可以联系ytkah

 

posted @   ytkah  阅读(528)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2015-05-09 网页出现scanstyles does nothing in Webkit / Mozilla的解决方法
2014-05-09 在iphone上安装多个微信 【微信营销必备】
网址导航 gg N / G Sitemap

部分内容来源于网络,如有版权问题请联系删除

  
点击右上角即可分享
微信分享提示