.net IIS设置301跳转、不带www,带www 都跳转到https://www
2025-04-23


需要安装IIS URL Rewrite Module 下载安装地址

官网地址:https://www.iis.net/downloads/microsoft/url-rewrite

在web.config中<system.webServer>中添加:

<rewrite>
  <rules>
  
    <!-- 所有请求跳转到 https://www.sunuer.com -->
    <rule name="Force HTTPS and www" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_HOST}" pattern="^sunuer\\.com$" />
      </conditions>
      <action type="Redirect" url="https://www.sunuer.com/{R:1}" redirectType="Permanent" />
    </rule>

  </rules>
</rewrite>