简体   繁体   中英

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 ". This works. But after placing an updated web.config in production, these are my results from typing in these into the 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:

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.

I would recommend enabling IIS Failed Request Tracing in order to identify exactly why your rewrite rule isn't working for those values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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