Home > front end >  IIS redirecting subdomain and an optional path
IIS redirecting subdomain and an optional path

Time:01-27

How can I redirect a subdomain to a path BUT the path may also contain the subdomain. It's not any subdomain so I don't need wildcards but a specific one so for example using 'test' as the subdomain:

test.example.com/test/this-is-a-test

or

test.example.com/this-is-a-test

which I'd like either one to redirect to:

example.com/test/this-is-a-test

This is what I have so far but I cannot get it to work:

<rule name="redirect test.example.com" patternSyntax="ECMAScript" stopProcessing="true">
    <match url=".*" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^test.example.com(/test)?" />
    </conditions>
    <action type="Redirect" redirectType="Permanent" url="https://www.example.com/test/{R:0}" appendQueryString="true" />
</rule>

CodePudding user response:

This should've been rather simple but due to my unfamiliarity with the IIS rewrite module it became complicated. I misunderstood one important thing which is match url=... is not actually the full URL but rather the path after domain.com/

So after that revelation it was easy to implement like so:

<match url="^(test)?/?(.*)" />
<conditions logicalGrouping="MatchAny">
    <add input="{HTTP_HOST}" pattern="^test.example.com" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.example.com/test/{R:2}" appendQueryString="true" />
  • Related