IIS Redirect non-www to www And http to https

I am trying to implement two rules for IIS for redirecting not 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:

Is there a way to combine this in one rule?

+7
asp.net-mvc iis
source share
1 answer

Yes, you can combine them into one and use logical grouping for conditions and set it to Any, which will be the equivalent of 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> 
+11
source

All Articles