Search code examples
url-rewritingiis-10url-rewrite-module

url rewrite for one domain to https


I have a IIS server running sites using 2 domains with multiple sites on each. I'm trying to write a url rewrite rule that only affects the one site The domains is this

*.it.test.com
*.test.com 

I have tried this rule but it seems to it do not seem to work - maybe I need a wildcard or something in the domian.

I'm trying to hit the subdomains of it.test.com

 <rule name="Redirect to HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" ignoreCase="true" negate="false" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{HTTP_HOST}" ignoreCase="true" matchType="Pattern"  pattern="^it.test.com$" />
      <add input="{HTTPS}"  pattern="off" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Temporary" />
  </rule>

A couple of sites names could be this

bb.it.test.com
aa.it.test.com
cc.test.com

Solution

  • As far as I know, if you choose patternSyntax="Wildcard", the {HTTP_HOST} in conditions is also use wildcard to match.

    enter image description here

    So this is reason why pattern="^it.test.com$" match failed. Wildcard is a special sentence, mainly including (*) and (?). Start with (^) and end with ($) is the usage of regular expressions.

    Match fail:

    enter image description here

    Even you change patternSyntax="regular expressions", it will still fail because of ^. bb.it.test.com is not start with it but bb.

    enter image description here


    Two solutions.

    1. Use regular expressions and set the pattern="(.*).it.test.com$", but it only match bb.it.test.com aa.it.test.com. pattern="(.*).test.com$" will match both of them.

    enter image description hereenter image description here

    1. Still use Wildcard but set the pattern="*.test.com".

    enter image description here