Adding an Inbound Rule in IIS [UrlRewite]

7.1k views Asked by At

Currently, on my Production server, i have Inbound Rule set as follows which works file for all my requirements.

Inbound Rule is as follows.

Match URL section
Requested URL: Matches the Pattern
Using: Regular Expression
Pattern: (.*)
Ignore Case: checked.

Conditions section
Logical grouping: Match All.
Input: {HTTPS}
Type: Matches the Pattern
Pattern: ^OFF$
Track capture groups across conditions: unchecked.

Action section:
Action type: Redirect
Redirect URL: https://www.domainname.com/{R:0}
Append query string: checked.
Redirect Type: Permanent (301).

My Requirement is: When user types

https://beta.domain.com it should redirect to https://www.domain.com. 

if user types

http://beta.domain.com it should redirect to https://www.domain.com. 

If user types

http://www.domain.com it should redirect to https://www.domain.com

If user types

http://domain.com it should redirect to https://www.domain.com

Thanks in advance. Any help would be appreciated.

Thanks in advance.

1

There are 1 answers

2
Marco Miltenburg On

Isn't that exactly what the conditional is suppose to do? It checks whether HTTPS is OFF and only then the redirect will happen. If HTTPS is ON it is not suppose to redirect otherwise you will end up in an endless redirect.

If you always want the hostname to be www.domainname.com you should add that as an additional conditional. E.g.:

<rule name="Force HTTPS and host name: www.domainname.com" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" negate="true" pattern="^ON$" />
        <add input="{HTTP_HOST}" negate="true" pattern="^www\.domainname\.com$" />
    </conditions>
    <action type="Redirect" url="https://www.domainname.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>

If either HTTPS is not ON or HTTP_HOST is not www.domainname.com it will redirect. Only if both are true, it will not redirect.