IIS URL重写:强制规范主机名和放大器; HTTP到HTTPS重定向 [英] IIS URL Rewriting: Enforce canonical hostname & HTTP to HTTPS redirect

查看:318
本文介绍了IIS URL重写:强制规范主机名和放大器; HTTP到HTTPS重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的web.config文件中使用这两个规则:

 <规则名称=强制规范主机名stopProcessing =真>
      <匹配URL =(。*)/>
      <条件>
        <添加输入={HTTP_HOST}否定=真模式=^ WWW \\ .mySite \\ .COM $/>
      &所述; /条件>
      <作用TYPE =重定向URL =htt​​ps://www.mySite.com/{R:1}redirectType =永久/>
    < /规则>
    <规则名称=HTTP到HTTPS重定向stopProcessing =真>
      <匹配URL =(。*)/>
      <条件>
        <添加输入={} HTTPS模式=关闭IGNORECASE =真/>
      &所述; /条件>
      <作用TYPE =重定向redirectType =找到URL =htt​​ps://开头{HTTP_HOST} / {R:1}/>
    < /规则>

通过这两个规则,我得到以下重定向到工作:


  1. http://www.mySite.com --->的 https://www.mysite.com

  2. HTTP://mysite.com--- > https://www.mysite.com

  3. https://mysite.com --->失败重定向到的 HTTPS://www.mysite.com...Why


解决方案

不知道如果你还在寻找一个答案,但在这里不用。某些搜索和反复试验后,我发现以下规则成功。我遇到的大多数的例子是与问候模式匹配不必要的复杂性,并介绍prevent按预期工作规则的其他变量。这些规则之下可以应用到任何网站,没有什么是硬codeD,以便它应该永远是直复制和粘贴的工作:

 <规则名称=重定向到WWWstopProcessing =真>
    <匹配URL =(。*)/>
    <条件>
        <添加输入={HTTP_HOST}模式=^ WWW \\。否定=TRUE/>
    &所述; /条件>
    <作用TYPE =重定向URL =htt​​ps://开头WWW {HTTP_HOST} {} HTTP_URLredirectType =永久appendQueryString =FALSE/>
< /规则>
<规则名称=重定向到HTTPS>
    <匹配URL =(。*)/>
    <条件>
        <添加输入={} HTTPS模式=OFF/>
    &所述; /条件>
    <作用TYPE =重定向URL =htt​​ps://开头{HTTP_HOST} {} HTTP_URLredirectType =永久appendQueryString =FALSE/>
< /规则>

有两点需要注意:被应用redirectType =永久将将导致规则,直到浏览器的历史/缓存清空;这应该是一件好事,因为浏览器会做的工作向前发展。此外,appendQueryString =假是必要的,因为{} HTTP_URL服务器变量已经包含了完整的查询字符串;缺省选项是真,并会导致在这里重复查询字符串

I'm using these two rules in my web.config file:

<rule name="Enforce canonical hostname" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^www\.mySite\.com$" />
      </conditions>
      <action type="Redirect" url="https://www.mySite.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>

With these two rules I get the following Redirect to work :

  1. http://www.mySite.com ---> https://www.mysite.com
  2. http://mysite.com--->https://www.mysite.com
  3. https://mysite.com ---> this fails to redirect to https://www.mysite.com...Why?

解决方案

Not sure if you're still seeking an answer but here goes. After some searching, and trial and error, I found success with the following rules. Most examples I've encountered are unnecessarily complex with regards to pattern matching and introduce other variables that prevent the rules from working as intended. The rules below can be applied to any website, and nothing is hard-coded so it should always be a straight copy-and-paste job:

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

Two things to note: redirectType="Permanent" will will result in the rule being applied until the browser's history / cache are emptied; this should be a good thing as the browser would do the work going forward. Also, appendQueryString="false" is necessary as the {HTTP_URL} server variable already includes the full querystring; by default the option is "true" and would result in duplicate querystrings here.

这篇关于IIS URL重写:强制规范主机名和放大器; HTTP到HTTPS重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆