简体   繁体   English

重写规则:有关正则表达式模式的问题

[英]Rewrite Rule: Question about regex pattern

I can't figure out why a certain rewrite rule does not work. 我不知道为什么某些重写规则不起作用。 I have this rule: 我有这个规则:

<rule name="CanonicalHostNameRule1" enabled="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="testsite\.com" negate="true" />
  </conditions>
  <action type="Redirect" url="https://www.testsite.com/{R:1}" />
</rule>

When I go into IIS and using the URL Rewrite module, I tested using " http://testsite.com ". 当我进入IIS并使用URL重写模块时,我使用“ http://testsite.com ”进行了测试。 This works. 这可行。 But after placing an updated web.config in production, these are my results from typing in these into the url: 但是将更新的web.config放入生产环境后,这些是我在url中键入以下内容的结果:

I was able to reach the site by typing in these variations: 通过输入以下变体形式,我可以访问该网站:

https://www.testsite.com
http://www.testsite.com
www.testsite.com
testsite.com 

But I received the error message "testsite.com refused to connect" when typing in: 但是我在输入时收到错误消息“ testsite.com拒绝连接”:

https://testsite.com
http://testsite.com

What am I missing, that is preventing the last 2 from working? 我缺少什么,这阻碍了最后两个工作? It seems like the rewrite rule I have should catch all requests. 似乎我拥有的重写规则应捕获所有请求。

Thank you in advance! 先感谢您!

My guess would be that for the last two examples, their {HTTP_HOST} will have no www which means that it will match your condition resulting in your negate attribute skipping your rule action. 我的猜测是,对于最后两个示例,它们的{HTTP_HOST}将没有www,这意味着它将与您的条件匹配,从而导致negate属性跳过规则操作。

I would recommend enabling IIS Failed Request Tracing in order to identify exactly why your rewrite rule isn't working for those values. 我建议您启用IIS Failed Request TracingIIS失败请求跟踪) ,以便准确确定为什么重写规则不适用于这些值。

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

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