6

I have an IIS7 web server with several sites running. Some of the sites are sub-domains of one domain and the others are totally separate domains. I want to use IIS re-write to redirect all the sub domain sites of one domain to https, but I want the other domains left as they are. For example I have the following sites on the same web server:

one.test.com, two.test.com, otherdomain.com

And I want to setup a global IIS rewrite to redirect http://one.test.com and http://two.test.com to https, but otherdomain.com should not be affected.

Here is what I have so far, and when I've tested the regex it seems correct but it is not redirecting the subdomain sites:

<rewrite>
            <globalRules>
                <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
                    <match url="(.*)(\.test\.com)" />
                    <conditions logicalGrouping="MatchAny">
                    </conditions>
                    <action type="Redirect" url="https://{R1}{R2}" redirectType="SeeOther" />
                </rule>
            </globalRules>
        </rewrite>

Am I over complicating this or missing something obvious?

Cheers.

7

You need to add conditions that match HTTP_HOST to your rule (the "url" variable in URL Rewrite doesn't include hostname).

<globalRules>
    <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="(.+)\.test\.com" />
        </conditions>
        <action type="Redirect" url="https://{C:0}/{R:0}" />
    </rule>
</globalRules>

This rule should redirect all requests on *.test.com to HTTPS.

2

You have to add this condition <add input="{HTTPS}" pattern="off" /> in the above solution. Otherwise it will ends in a loop. So the rule will be as follow,

<globalRules>
    <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
            <add input="{HTTPS}" pattern="off" />
            <add input="{HTTP_HOST}" pattern="(.+)\.test\.com" />
        </conditions>
        <action type="Redirect" url="https://{C:0}/{R:0}" />
    </rule>
</globalRules>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.