15

i'm trying to implement two rules for IIS to Redirect non-WWW to WWW and http to https.

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

So, i added this to my web.config:

  <system.webServer>
<rewrite xdt:Transform="Insert">
  <rules>
    <rule name="Force WWW" enabled="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^[^www]" />
      </conditions>
      <action type="Redirect" url="https://www.zzz.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    <rule name="Force HTTPS" enabled="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://www.zzz.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>    

My Question:

Is there any way to combine this in one rule?

tereško
  • 58,060
  • 25
  • 98
  • 150
Noah Andreas
  • 313
  • 1
  • 3
  • 10

1 Answers1

34

Yes you can merge them into one and use the logicalGrouping for the conditions and set it to Any which would be the equivalent of an "OR". For example:

<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny">
      <add input="{HTTP_HOST}" pattern="^[^www]" />
      <add input="{HTTPS}" pattern="off" />
  </conditions>
  <action type="Redirect" url="https://www.zzz.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
Carlos Aguilar Mares
  • 13,411
  • 2
  • 39
  • 36
  • Hi when I try this for my own problem win 2012 IIS 8 when I try to go to https://example.com/ then it errors. http://example.com goes to https://www.example.com and example.com but https://example.com just takes me to a This site can’t be reached example.com’s server DNS address could not be found. Try: Reloading the page Checking the connection Checking the proxy, firewall and DNS configuration ERR_NAME_NOT_RESOLVED – MonkeyMagix Jul 22 '16 at 13:03
  • 1
    I have 4 bindings setup by the way 1 with www for http and https and one without www – MonkeyMagix Jul 22 '16 at 13:03
  • 2
    You do need 4 bindings. IIS needs a binding for each "bad" version of the domain as well as the preferred domain. e.g. `http://zzz.com, https://zzz.com, http://www.zzz.com, https://www.zzz.com`. – Phil M Aug 25 '16 at 13:22
  • 3
    @carlos: how would you make the domain name in this example dynamic and preserve that in the redirect? I want to use your example, but I now have this condition: `` and this action `` help is appreciated! – Adam Nov 28 '17 at 10:37
  • @PhilM the binding reminder is primo. It's easy for me to forget this. – secretwep Jun 07 '18 at 16:44
  • @MonkeyMagix your answer is working, but is there any solution via url rewriting ? because there is problem with https://example.com when you bind website with "www" – Jigar Sangoi Jul 22 '21 at 09:39