IIS将非www重定向到www AND http到https

Noa*_*eas 13 iis asp.net-mvc

我正在尝试为IIS实现两个规则,将非WWW重定向到WWW,将http重定向到https.

http://zzz.com -> https://www.zzz.com
http://www.zzz.com -> https://www.zzz.com
https://zzz.com -> https://www.zzz.com
Run Code Online (Sandbox Code Playgroud)

所以,我把它添加到我的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>    
Run Code Online (Sandbox Code Playgroud)

我的问题:

有没有办法将这个结合在一个规则中?

Car*_*res 26

是的,您可以将它们合并为一个并使用logicalGrouping作为条件,并将其设置为Any,它等同于"OR".例如:

<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>
Run Code Online (Sandbox Code Playgroud)

  • @carlos:如何在此示例中使域名动态化并将其保留在重定向中?我想使用您的示例,但现在有以下条件:`&lt;添加输入=“ {HTTP_HOST}” pattern =“ ^ [^ \。] + \。[^ \。] + $” /&gt;`和此操作感谢您提供&lt;action type =“ Redirect” url =“ https:// www。{HTTP_HOST} / {R:0}” /&gt;`的帮助! (3认同)
  • 您确实需要4个绑定。IIS需要为域的每个“不良”版本以及首选域进行绑定。例如“ http://zzz.com、https://zzz.com、http://www.zzz.com、https://www.zzz.com”。 (2认同)