非 www 到 www 的 IIS 重写规则不起作用

IIS Rewrite rule for non-www to www not working

我们使用 Flex 在 IIS 中托管了很多网站。他们每个人都有很多 Rewrite/Redirect 规则。其中之一现在需要重写规则以将非 www url 定向到 www url。已经尝试了来自各种站点的相当多的流行解决方案,包括其他 SO 问题。我最后尝试的是如下,没有太多用处:

<rule name="Redirect to WWW" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^your.domain.name$" />
      </conditions>
      <action type="Redirect" url="http://www.your.domain.name/{R:0}" redirectType="Permanent" />
</rule>
  1. 确保您拥有最新的 URL 重写扩展。
    https://www.iis.net/downloads/microsoft/url-rewrite
  2. 测试结果前,请清理本地缓存。我们最好私下验证一下window。

此外,Failed Request Tracing 是解决请求处理故障的强大工具。 FRT 可以与 URL 重写模块一起使用,以跟踪如何将重写规则应用于请求 URL。
https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules
最后,请尝试以下配置。

<rule name="Force www" enabled="true" stopProcessing="false">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern="^example\.com$" />
        </conditions>
        <action type="Redirect" url="https://www.example.com{REQUEST_URI}" />
</rule>

如果问题仍然存在,请随时告诉我。