即使模式匹配,也会跳过IIS重写规则

对于模糊的标题,对不起,这个问题太复杂了,不能总结成一个短语。

我正在尝试设置以下redirect规则:

  1. blog.mydomain.net/en/something :redirect到www.mydomain.com/something
  2. blog.mydomain.net/fr/something :redirect到www.mydomain.fr/something
  3. blog.mydomain.net/* :redirect到www.mydomain.com

规则3正在起作用,但规则1和规则2似乎被跳过,所以总是适用规则3。 这是我的web.config规则:

 <!-- Canonicalize mydomain.com to www.mydomain.com --> <rule name="CanonicalHostNameRule_en" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_HOST}" pattern="^mydomain\.com$" /> </conditions> <action type="Redirect" url="http://www.mydomain.com/{R:1}" /> </rule> <!-- Canonicalize mydomain.fr to www.mydomain.fr --> <rule name="CanonicalHostNameRule_fr" enabled="true" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_HOST}" pattern="^mydomain\.fr$" /> </conditions> <action type="Redirect" url="http://www.mydomain.fr/{R:1}" /> </rule> <!-- Redirect blog.mydomain.net/en/something to www.mydomain.com/something --> <rule name="RedirectBlog_en" enabled="true" stopProcessing="true"> <match url="^/en(/.*)?$" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" /> </conditions> <action type="Redirect" url="http://www.mydomain.com/{R:1}" /> </rule> <!-- Redirect blog.mydomain.net/fr/something to www.mydomain.fr/something --> <rule name="RedirectBlog_fr" enabled="true" stopProcessing="true"> <match url="^/fr(/.*)?$" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" /> </conditions> <action type="Redirect" url="http://www.mydomain.fr/{R:1}" /> </rule> <!-- Redirect blog.mydomain.net/* to www.mydomain.com --> <rule name="RedirectBlog_other" enabled="true" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" /> </conditions> <action type="Redirect" url="http://www.mydomain.com/" /> </rule> <!-- WordPress-specific rules --> ... 

我不明白为什么规则RedirectBlog_enRedirectBlog_fr被跳过; 我testing了正则expression式,他们工作正常。

任何人都可以发现问题吗?


编辑:如果我禁用第三条规则(RedirectBlog_other),然后规则1和2工作正常…怎么可能,因为规则1和2在规则3之前执行?

好,我知道了!

首先,事情并没有像我想的那样发生。 当我禁用规则3时,规则1和规则2不起作用:我仍然被redirect到了我的实际域,但这是通过Wordpress完成的,而不是由我的规则完成的。

其次,我的匹配URL的模式是错误的:领先的“/” 包含在input中,所以我的规则根本不匹配。