简体   繁体   English

IIS URL 重写规则不适用于整个站点

[英]IIS URL rewriting rule not working for entire site

My target is to forward the following URL's to the new site (exact matches is needed since I have other rules with oldsite.com/test.aspx):我的目标是将以下 URL 转发到新站点(需要完全匹配,因为我对 oldsite.com/test.aspx 有其他规则):

  • oldsite.com oldsite.com
  • oldsite.com/ oldsite.com/
  • www.oldsite.com www.oldsite.com
  • www.oldsite.com/ www.oldsite.com/
  • Only the ones from above (statically) i do not want to forward oldsite.com/test.aspx for example例如,只有上面的(静态)我不想转发 oldsite.com/test.aspx

What I used is:我用的是:

<rule name="Redirect" stopProcessing="true">
                    <match url="oldsite.com$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{HTTP_HOST}" pattern="oldsite.com" />
                        <add input="{HTTP_HOST}" pattern="www.oldsite.com" />
                        <add input="{HTTP_HOST}" pattern="oldsite.com/" />
                        <add input="{HTTP_HOST}" pattern="www.oldsite.com/" />
                    </conditions>
                    <action type="Redirect" url="http://newsite.com" appendQueryString="false" />
                </rule>

My rule doesn't work and I was wondering why我的规则不起作用,我想知道为什么

An HTTP Host header never contains a / , so your {HTTP_HOST} conditions ending with / are redundant. HTTP Host 标头从不包含/ ,因此以/结尾的{HTTP_HOST}条件是多余的。

<match url="oldsite.com$" on the other hand simply means that match only with paths (not URLs) ending with oldsite.com . <match url="oldsite.com$"另一方面只是意味着只匹配以oldsite.com结尾的路径(而不是 URL)。

eg:例如:

  • http://whatever/something-oldsite.com matches. http://whatever/something-oldsite.com匹配。
  • http://whatever/oldsite.com matches. http://whatever/oldsite.com匹配。
  • http://whatever/oldsite.comA not matches. http://whatever/oldsite.comA不匹配。
  • http://whatever/oldsite.co not matches. http://whatever/oldsite.co不匹配。

Apparently this is not what you want.显然这不是你想要的。

Besides, bear in mind that a literal dot character ( . ) means "match with any character" in Regular Expressions.此外,请记住,在正则表达式中,文字点字符 ( . ) 表示“与任何字符匹配”。 This unexpectedly causes your pattern to match match with an URL like http://whatever/oldsiteZcom for example.例如,这会意外地导致您的模式与诸如http://whatever/oldsiteZcom类的 URL 匹配。 Remember to escape dots with backslashes in the patterns when you are looking for literal dots.当您寻找文字点时,请记住在模式中使用反斜杠转义点。

The url attribute of <match> element is here a little bit ambiguous, I always wonder why Microsoft choose this name, something like <match path=".. would be clearer but this is what we have with the URL Rewrite module. I understand your confusion, I was there. <match>元素的url属性在这里有点模棱两可,我一直想知道为什么微软选择这个名字,比如<match path="..你的困惑,我在那里。

Anyways, you're looking for exact empty paths so you need a rule like below.无论如何,您正在寻找确切的空路径,因此您需要如下规则。

<rule name="Redirect" stopProcessing="true">
    <match url="^$" ignoreCase="false" /> <!-- path is empty, just starts (^) and ends ($), does not contain anything -->
    <conditions>
        <add input="{HTTP_HOST}" pattern="^oldsite\.com$" /> <!-- host name literally equals to oldsite.com -->
        <add input="{HTTP_HOST}" pattern="^www\.oldsite\.com$" /> <!-- host name literally equals to www.oldsite.com -->
    </conditions>
    <action type="Redirect" url="http://newsite.com" appendQueryString="false" />
</rule>

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM