0
votes

I have an application I wrote in ASP.NET C#. Two domains point to the site. In the web.config I'd like a url rewrite so if http://domain.com is entered it redirects to https://www.domain.com

In addition if http://domain2.com is entered it should redirect to https://www.domain2.com

I see a bunch of examples when its just one domain. Wondering if its possible when 2 domains point to the same site. When I tried I got redirect loops.

1
Can you just create a 2nd web site with bindings for the 2nd domain and do the regular redirect there?David
No, because i want to manage one piece of source in one directory. So the site would be in c:\inetpub\wwwroot\ and I want www.domain1.com and www.domain2.com to point to the same directory...using https as described above.Vibration Of Life

1 Answers

1
votes

You can try something like this:

<rule name="NON-WWW HTTPS DOMAIN" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^domain\.com$" />
    <add input="{HTTPS}" pattern="^ON$" />
  </conditions>
  <action type="Redirect" url="https://www.domain.com/{R:0}" />
</rule>
<rule name="Redirect to domain HTTPS" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^[www\.]*domain\.com$" />
    <add input="{HTTPS}" pattern="^OFF$" />
  </conditions>
  <action type="Redirect" url="https://www.domain.com/{R:0}" />
</rule>
<rule name="Redirect to domain2 HTTPS" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^[www\.]*domain2\.com$" />
    <add input="{HTTPS}" pattern="^OFF$" />
  </conditions>
  <action type="Redirect" url="https://www.domain2.com/{R:0}" />
</rule>
<rule name="NON-WWW HTTPS DOMAIN 2" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^domain2\.com$" />
    <add input="{HTTPS}" pattern="^ON$" />
  </conditions>
  <action type="Redirect" url="https://www.domain2.com/{R:0}" />
</rule>

In order to redirect https://domain.com or https://domain2.com to https://www.domain.com and https://www.domain2.com you need to enable https for: domain.com/www.domain.com and domain2.com/www.domain2.com. It must be enabled in IIS because otherwise IIS will block request for not binded https request and the request will not even be processed by rewrite url.