tags:

views:

10

answers:

1

I'm trying to write IIS7 URL Rewrites rules that do two things:

  • If a request is to http, force it to https
  • If the url has a "www" in it, remove it and redirect to different url

In both cases I want to redirect to https://my.domain.com (substitute for real domain name)

I have no problem with the http to https rule. Also, the case of http://www.my.domain.com to https://my.domain.com also works. However, the one case I have not been able to get to work is when the original request is to https://www.my.domain.com

Here's what I have now:

<rule name="r2" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_PORT}" pattern="443" negate="false"  matchType="Pattern" />
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$"  matchType="Pattern"  />       
</conditions>
<action type="Redirect" url="https://my.domain.com" />
</rule>


<rule name="r1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_PORT}" pattern="443" negate="true"  matchType="Pattern" />
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$"  matchType="Pattern"  />
</conditions>
<action type="Redirect" url="https://my.domain.com" />
</rule>

Any idea of what I need to change to get https://www.my.domain.com to redirect to https://my.domain.com ?

A: 

This may work for you

<rule name="Canonical Host Name" stopProcessing="true">
                        <match url="(.*)"/>
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="https://mydomain.com$"/&gt;
                        </conditions>
                        <action type="Redirect" url="https://www.mydomain.com/{R:1}" redirectType="Permanent"/>
                    </rule>
PaulStack