Web.config文件。重定向所有流量www.my ...使用规则元素。 [英] Web.config. Redirect all traffic to www.my... Using rules element.

查看:239
本文介绍了Web.config文件。重定向所有流量www.my ...使用规则元素。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web.config文件自动发送流量到HTTPS。但是,如果有人在MyDomain.com进入,然后它会去 https://mydomain.com ,如果​​有人进入www.mydomain.com那么它会去 https://www.mydomain.com

我希望所有的交通去 https://www.mydomain.com 。这可能使用在web.config规则元素?我的文件目前看起来是这样的:

 <&改写GT;
  <规则与GT;
      <规则名称=HTTP到HTTPS重定向stopProcessing =真>
          <匹配URL =(。*)/>
          <条件>
              <添加输入={} HTTPS模式=关闭IGNORECASE =真/>
          &所述; /条件>
          <作用TYPE =重定向redirectType =找到URL =htt​​ps://开头{HTTP_HOST} / {R:1}/>
      < /规则>
  < /规则>


解决方案

的规则

 <规则名称=重定向到www子>  <*匹配URL = />  <条件logicalGrouping =MatchAll>
    <添加输入={HTTP_HOST}模式=^(WWW \\)$(*)否定=TRUE/>
    <添加输入={} SERVER_PROTOCOL模式=^(/.*)$(*)?/>
  &所述; /条件>  <作用TYPE =重定向URL ={C:1}:// WWW {HTTP_HOST} / {R:0}redirectType =永久/>< /规则>

规则说明


  • <匹配/> 限制规则只要求其路径和查询字符串匹配给定的模式。在我们的例子中,我们希望,因为我们将重定向基于域匹配所有路径和查询字符串。


  • <条件/> 进一步限制的规则,只满足给定条件匹配的请求。第一个条件排除其域名已经与WWW开始的请求。第二个条件是存在的只是为{C:1}。逆向引用,它不应该过滤掉任何


  • <作用> prependsWWW。域,然后重定向。


变量


  • {R:0} 是一个逆向引用从全场比赛的<匹配\\> 标记。向后引用应该只包含路径和查询字符串,因为这是所有<匹配\\> 以往比赛反对


  • {C:1} 是最终条件的第一个匹配组反向引用。这应该包含一切都交给了/,在{} SERVER_PROTOCOL变量。


  • {HTTP_HOST} 是包含请求域的服务器变量。 (请参见的完整列表href=\"http://msdn.microsoft.com/en-us/library/ms524602%28v=vs.90%29.aspx\">。)


  • {SERVER_PROTOCOL} 其他服务器变量。它的格式应为{协议} / {版本号}。


其他选项


  • <作用redirectType> 可以是临时的,找到或SeeOther。 (请参见这里更多信息。)


  • <条件logicalGrouping> 可MatchAll或MatchAny。


结论

有关更完整的解释,请参阅<一个href=\"http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference\">here.

I have a web.config file which automatically sends traffic to HTTPS. However, if someone enters in MyDomain.com then it will go to https://mydomain.com and if someone enters www.mydomain.com then it will go to https://www.mydomain.com.

I want ALL traffic to go to https://www.mydomain.com. Is this possible using the rules element of the web.config? My file currently looks like this:

<rewrite>
  <rules>
      <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>
  </rules>

解决方案

The Rule

<rule name="Redirect to www subdomain">

  <match url=".*" />

  <conditions logicalGrouping="MatchAll">
    <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
    <add input="{SERVER_PROTOCOL}" pattern="^(.*)(/.*)?$"/>
  </conditions>

  <action type="Redirect" url="{C:1}://www.{HTTP_HOST}/{R:0}" redirectType="Permanent"/>

</rule>

Explanation of Rule

  • <match /> Limits a rule to only requests whose path and query string match the given pattern. In our case we want to match all paths and query strings since we will redirect based on domain.

  • <conditions /> Limits a rule even further to just the matched requests that satisfy the given conditions. The first condition excludes requests whose domain already starts with "www". The second condition is there just for the {C:1} backreference and it shouldn't filter out anything.

  • <action> prepends "www." to the domain and then redirects.

Variables

  • {R:0} is a backreference to the full match from the <match \> tag. The back-reference should only contain the path and query string since that is all that <match \> ever matches against.

  • {C:1} is a backreference to the first match group from the final condition. This should contain everything up to the "/" in the {SERVER_PROTOCOL} variable.

  • {HTTP_HOST} is a server variable that contains the requested domain. (See here for a full list.)

  • {SERVER_PROTOCOL} another server variable. Its format should be "{protocol}/{version number}".

Other Options

  • <action redirectType> can be Temporary, Found or SeeOther. (See here for more info.)

  • <conditions logicalGrouping> can be MatchAll or MatchAny.

Conclusion

For a more complete explanation please see here.

这篇关于Web.config文件。重定向所有流量www.my ...使用规则元素。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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