IIS重写规则的组合-重定向到更改的域名,删除主机名前缀重定向到ssl

Combination of IIS rewrite rules - redirect to changed domain name, remove hostname prefix redirect to ssl

我的 IIS 网站目前以 http://mywebsite.com 访问,我有 3 个与 url 重写相关的目标:

  1. 重定向到网站的新名称,mynewwebsite.com
  2. 删除 www 前缀(如 www.mywebsite.com -> mywebsite.com 或 www.mynewwebsite.com --> mynewwebsite.com
  3. 重定向到 SSL

所以总而言之,我想将所有入站请求重定向到 https://mynewwebsite.com/anything*.

必须考虑的输入案例是

我的重写规则如下:

<rewrite>
  <rules>
    <rule name="Do stuff" enabled="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="off" />
        <add input="{HTTP_HOST}" pattern="mywebsite.com" />
        <add input="{HTTPS}" pattern="^www.mynewwebsite\.com$" />
      </conditions>
      <action type="Redirect" url="https://mynewwebsite.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

使用此规则集,以下输入案例失败(未删除 www):https://www.mynewwebsite.com/anything*

我已经充分反对这一点,以至于我质疑我对规则定义如何工作的基本理解。

我得出了以下有效的规则,但恐怕这是巧合,而不是我真正理解发生了什么。欢迎评论。

<rewrite>
  <rules>
    <rule name =" Force HTTPS" enabled="true">
      <match url =" (.*)" ignoreCase= "false " />
      <conditions logicalGrouping =" MatchAny" >
        <add input =" {HTTP_HOST}" negate="true" pattern =" ^mynewwebsite\.com$" />
        <add input =" {HTTPS}" pattern= "^www.mynewwebsite\.com$ " />
        </conditions>
      <action type =" Redirect" url= "https://mynewwebsite.fm/{R:1} " appendQueryString="true" redirectType =" Permanent" />
    </rule>
  </rules>
</rewrite>