随着https的流行,很多使用IIS管理器的小伙伴都在自已的主机上面导入了SSL安全证书,可以通过http和https两个方法访问。但这样对搜索引擎不友好。用http访问又会自动跳转到https,那有没有方法可以实现自动跳转呢。 有,以下是道川公司使用的方法:
第一、IIS需要安装URL ReWrite扩展插件。
第二、导入或写入URL重定向的规则文件,代码如下:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^0733.com.cn [NC]
RewriteRule ^(.*)$ https://www.0733.com.cn/$1 [L,R=301]
为了更好的识别跳转代码,建议加在.htaccess文件顶部。
第三:Web.Config文件配置
打开项目的Web.Config配置文件,在<system.webServer>中,配置如下重定向代码:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="已导入的规则 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_PORT}" pattern="80" ignoreCase="false" />
<add input="{HTTP_HOST}" pattern="^0733.com.cn" />
</conditions>
<action type="Redirect" url="https://www.0733.com.cn/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>