3

IIS 7.5中的URL Rewrite 2.0的上下文中,我希望能够以尽可能少的规则为多国站点的多个域强制执行规范域名。像这样的东西:

<rule name="UK Host Name">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^company\.co\.uk$" />
        <add input="{HTTP_HOST}" pattern="^company\.co$" />
        <add input="{HTTP_HOST}" pattern="^company\.org$" />
        <add input="{HTTP_HOST}" pattern="^company\.net$" />
        <add input="{HTTP_HOST}" pattern="^company\.uk\.com$" />
        <add input="{HTTP_HOST}" pattern="^www\.company\.co\.uk$" negate="true" />
    </conditions>
    <action type="Redirect" url="http://www.company.co.uk/{R:1}" />
</rule>
<rule name="France Host Name">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^company\.fr$" />
        <add input="{HTTP_HOST}" pattern="^company-france\.com$" />
        <add input="{HTTP_HOST}" pattern="^www\.company\.fr$" negate="true" />
    </conditions>
    <action type="Redirect" url="http://www.company.fr/{R:1}" />
</rule>

我认为,上述问题中的每一个条件都必须为真logicalGrouping="MatchAll",但如果更改为MatchAny最后一个条件(with negate="true")将被忽略,这意味着即使用户正在访问正确的域,我们也会运行重定向规则。

我能想到的唯一选择是为每个不同的域设置一个单独的重写规则,但是可能有大量的域并且它可能会变得混乱。将会有很多其他的重写规则和映射。

如何创建更复杂的条件集,而不仅仅是 All 或 Any?

4

1 回答 1

7

The trick is to combine the domains you want to match against into one rule with the or | operator so that you only have one 'positive' rule and one 'negative' rule which should MatchAll. E.g.:

<rule name="UK Host Name">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^company\.(co(\.uk)?|org|net|uk\.com)$" />
        <add input="{HTTP_HOST}" pattern="^www\.company\.co\.uk$" negate="true" />
    </conditions>
    <action type="Redirect" url="http://www.company.co.uk/{R:1}" />
</rule>
<rule name="France Host Name">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^company(-france)?\.(fr|com)$" />
        <add input="{HTTP_HOST}" pattern="^www\.company\.fr$" negate="true" />
    </conditions>
    <action type="Redirect" url="http://www.company.fr/{R:1}" />
</rule>

This might eventually give you a slight chance that your regular expression matches too many domain names. E.g. the pattern pattern="^company(-france)?\.(fr|com)$" also matches company.com which might not be desirable. In that case you have to be more specific and e.g. change the pattern to pattern="^company\.fr|company-france\.com$".

于 2013-08-15T22:02:46.237 回答