0
votes

IIS 10 server behind an AWS application load balancer will not redirect traffic for domain without www when client requests http rather than https. The rule to redirect traffic when www is specified works fine, but 404 is returned if you try the same url without www.

So:

  1. Enter "http://dname.com/blog" = 404

  2. Enter "http://www.dname.com/blog" = redirect to "https://www.dname.com/blog"

            <rule name="Force HTTPS" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
                    <add input="{HTTP_HOST}" pattern="^dname\.com$" />
                </conditions>
                <action type="Rewrite" url="https://www.dname.com{REQUEST_URI}" />
            </rule>
            <rule name="Force WWW HTTPS" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
                    <add input="{HTTP_HOST}" pattern="^www\.dname\.com$" />
                </conditions>
                <action type="Redirect" url="https://www.dname.com{REQUEST_URI}" />
            </rule>
    
3

3 Answers

1
votes

Nothing worked for me even after going through the answers provided on different forums. After 2 days of banging my head in this here's what I found which solved the issue :

  1. Bindings : Port 80 must be enabled (This can be added in bindings section in IIS).

  2. SSL settings : Required SSL must be unchecked.

  3. Add Rule :

<rewrite>
    <rules>
        <rule name="http to https redirection" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" 
                appendQueryString="false" />
        </rule>
    </rules>
</rewrite>
  1. Verify web config as it should reflect the rule added in IIS.
0
votes

There is a very very important step that should take care, before setup a redirect configure.

in web Sites project --> Actions(in the right) --> Bindings , the content will like below: Binding Content

You take carefully the yellow color part, the yellow part is your original web IP address. This original IP address must exist in "Site Bindings", without the yellow part the URL redirect will not working anymore.

The following config is my current IIS URL redirect setting:

    <rewrite>
        <globalRules>
            <rule name="Http redirect to Https" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTP_HOST}" pattern="localhost:8080" />   <-- the Red one should match above Site Bindings original IP address
                </conditions>
                <action type="Redirect" url="https://Your-Host-Name/{R:1}" redirectType="SeeOther" />
            </rule>
        </globalRules>
    </rewrite>
0
votes

I don't know why the previously posted rules wouldn't work, but I was able to create a refined rule that is working:

            <rule name="Force HTTPS" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{HTTP_X_Forwarded_Proto}" pattern="^https$" negate="true" />
                    <add input="{HTTP_HOST}" pattern="^(www\.)?dname\.com$" />
                </conditions>
                <action type="Redirect" url="https://www.dname.com{REQUEST_URI}" />
            </rule>

The above rule combines the two rules instead of looking for the domain without the www and then with the www in a separate rule. The regex (www\.) tells the rule to look for "www." and the question mark tells it that it may or may not be there, so that includes the domain with and without the www.