0
votes

I'm trying to redirect my site from WWW -> root.

http://www.example.com -> http://example.com https://www.example.com -> https://example.com

i've used the following rewrite rules:

<rule name="CanonicalHostNameHttp" stopProcessing="true">
<match url="(.*)" />  
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="http://example.com/{R:1}" />
</rule>

<rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>

This works fine:

This fails:

  • I enter https://www.example.com
  • It never redirects to https://example.com
  • And as a result it gives me a certificate error (because my certificate is for example.com not www.example.com)

What am I missing???

1

1 Answers

1
votes

How about buying a cheap cert for www.example.com as mentioned here... invalid security certificate and domain names ...and then set up a rule to redirect https://www.example.com -> https://example.com?

I have a similar problem at the moment. The problem is I can't install a second cert because Amazon EC2 limits you to a single public IP address per instance, and an IP address can only be bound to one cert. The only option I can think of is to change the DNS of my non-canonicalised domain to point at another server, install the second cert there, and then get that to redirect to my EC2 instance.