asp.net – IIS URL重写:强制规范主机名和HTTP到HTTPS重定向
发布时间:2020-12-15 23:16:30 所属栏目:asp.Net 来源:网络整理
导读:我在web.config文件中使用这两个规则: rule name="Enforce canonical hostname" stopProcessing="true" match url="(.*)" / conditions add input="{HTTP_HOST}" negate="true" pattern="^www.example.com$" / /conditions action type="Redirect" url="h
我在web.config文件中使用这两个规则:
<rule name="Enforce canonical hostname" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" negate="true" pattern="^www.example.com$" /> </conditions> <action type="Redirect" url="https://www.example.com/{R:1}" 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://{HTTP_HOST}/{R:1}" /> </rule> 有了这两个规则,我得到以下重定向工作: > http://www.example.com —> https://www.example.com 解决方法
不知道你还在寻找答案,但是这里.经过一番搜索,试错,我发现以下规则成功.我遇到的大多数例子在模式匹配方面是不必要的复杂的,并引入了阻止规则按预期工作的其他变量.以下规则可以应用于任何网站,没有什么是硬编码,因此它应该始终是一个直接的复制和粘贴作业:
<rule name="Redirect to WWW" stopProcessing="true" > <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^www." negate="true"/> </conditions> <action type="Redirect" url="https://www.{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false" /> </rule> <rule name="Redirect to HTTPS"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="OFF"/> </conditions> <action type="Redirect" url="https://{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false" /> </rule> 需要注意的两个事项:redirectType =“Permanent”将导致规则被应用,直到浏览器的历史/缓存清空;这应该是一件好事,因为浏览器会进行下一步的工作.此外,appendQueryString =“false”是必需的,因为{HTTP_URL}服务器变量已经包含完整的querystring;默认情况下该选项为“true”,并在此处导致重复的查询字符串. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET Forms身份验证和持久身份验证Cookie安全性
- asp.net-mvc – 在MVC/ASP.NET MVC中正确使用Model vs Cont
- asp.net – 以编程方式编辑Web.config
- ASP.NET MVC – 如何从局部视图中获取当前操作?
- asp.net-mvc – MVC3 ModelBinding回收了一个带有索引间隙的
- Asp.net:实现自动注销功能
- asp.net-mvc – 防止在ASP.NET MVC中缓存属性,每次执行一个
- asp.net – MVC 4数据注释“显示”属性
- asp.net-mvc-2 – 使用routeValue从MVC URL中删除索引
- asp.net-mvc – 从Api控制器内生成绝对的url to action