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文件,这个就是设置跳转规则的文件

<?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

<?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规则,整合代码如下

<?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

原文地址:https://www.cnblogs.com/ytkah/p/10838893.html