如何在web.config中将非WWW重定向到WWW

时间:2018-12-08 00:09:33

标签: c# web iis web-config url-redirection

我在这里尝试过web.config redirect non-www to www

并尝试:

<rule name="ensurewww" enabled="false" stopProcessing="true">
    <match url=".*" />
    <conditions>
         <add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
    </conditions>
    <action type="Redirect" url="{C:1}://www.{C:2}/{R:1}" redirectType="Permanent" />
</rule>
  

结果:1​​. abc.com ---> www.abc.com 真实

     

结果2. abc.com/a.aspx--->www.abc.com/a.aspx 错误

     

结果3. abc / com / abc ----> www.abc.com/abc 错误

     

最后:我要结果2和结果3为真

1 个答案:

答案 0 :(得分:1)

尝试:IIS Redirect non-www to www AND http to https

<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.obu.vn/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
  

它将将非www重定向到www并将http重定向到https