Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS Redirect non-www to www AND http to https

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?

like image 822
Noah Andreas Avatar asked Sep 09 '15 22:09

Noah Andreas


People also ask

How do I redirect non www urls to WWW in IIS?

First, open IIS Manager and double-click on the “URL Rewrite” icon. Next, click on “Add Rules…” from the Actions pane. Here you'll have a choice from a few wizard options, and with URL Rewrite 2.0 you can also create outbound rules. Create a Blank rule (inbound rules).

How do I redirect non www to web config?

Replace the “yourdomain” with your domain name and add it under the system. webServer section in the Web. Config, and that's it.


1 Answers

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>
like image 90
Carlos Aguilar Mares Avatar answered Oct 17 '22 17:10

Carlos Aguilar Mares