简体   繁体   中英

web.config to redirect multiple aliases across HTTP and HTTPS

My application has multiple aliases that all need to be redirected to our new alias. With my current rules, I'm able to get the redirects working, but only in one protocol or the other, not both.

For example, my web.cong rule looks like this:

<rule name="Redirect_Alias_1" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="*example1.com" />
    </conditions>
    <action type="Redirect" url="http://example2.com" />
</rule>

But if the user is trying to access our HTTPS version of example2, they get redirected back to the HTTP version. How do I write the rule so that the protocol doesn't matter?

Try 2 rules, each with a condition to check the HTTPS server variable.

<rule name="Redirect_Alias_1" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions>
        <add input="{HTTPS}" pattern="OFF" />
        <add input="{HTTP_HOST}" pattern="*example1.com" />
    </conditions>
    <action type="Redirect" url="http://example2.com" />
</rule>
<rule name="Redirect_Alias_2" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions>
        <add input="{HTTPS}" pattern="ON" />
        <add input="{HTTP_HOST}" pattern="*example1.com" />
    </conditions>
    <action type="Redirect" url="https://example2.com" />
</rule>

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