【问题标题】:IIS rewrite rule Redirect Non-www to dynamic Domain Equivalent and always httpsIIS 重写规则将非 www 重定向到动态域等效且始终为 https
【发布时间】:2023-03-07 22:44:09
【问题描述】:

我想要的是所有非 https 或没有 www 前置的请求都重定向到:"https://www." + domain name + possible query string parameters

我有这个重写规则 (found here):

<rule name="non-www to www https" enabled="true" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
    <add input="{HTTPS}" pattern="on" />
  </conditions>
  <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
</rule>

但是,当在浏览器地址栏中键入以下域时,不会发生重定向(并且我收到安全证书错误,因为我没有通配符 DNS SSL 证书):
https://example.com/
@ 987654323@

但是example.com(没有协议),正确重定向到https://www.example.com/

还要注意,在上述规则中,我动态匹配主机名,而不仅仅是在“example.com”上,因为我希望此规则适用于多个域名。

然后我还检查了这个post,它有一个简洁的规则:

<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>

我认为这正是我想要的,但是我如何使本示例中的域名动态并在重定向中保留它(就像第一个代码示例一样)? (原发帖者在过去 6 个月内没有登录,所以我在这里问)

此外,我还检查了this post,这似乎也是一个不错的候选:

<rule name="Redirect top domains with non-www to www" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
        <add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
        <add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
        <add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
      <serverVariables>
        <set name="Redirect" value="false" />
      </serverVariables>
 </rule>


<rule name="Force HTTPS" enabled="true" stopProcessing="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
        <add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
        <add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
 </rule>

但随后http://example.com 重定向到https://example.com,我仍然得到安全异常。

【问题讨论】:

    标签: ssl redirect iis url-rewriting web-config


    【解决方案1】:

    首先,我强烈建议您获取支持example.comwww.example.com 的新SSL 证书。对于大多数 SSL 提供商来说,这种证书实际上是相当标准的,它不必是通配符证书。否则你将无法像现在一样处理对https://example.com 的请求,我认为这是一个问题。

    您的前两条规则应与以下规则类似。

    P.S. 301 重定向会被浏览器缓存一段时间。在测试新规则之前,请为您的浏览器搜索 Google clear 301 redirect cache

    <rule name="All HTTP to HTTPS+WWW" stopProcessing="true">
        <match url=".*" />
        <conditions trackAllCaptures="true">
            <add input="{SERVER_PORT_SECURE}" pattern="0" />
            <add input="{HTTP_HOST}" pattern="(?:localhost|stage\.|dev\.)" negate="true" />
            <!-- here with this 3rd condition we capture the host name without "www." prefix into {C:1} variable to use in redirect action -->
            <add input="{HTTP_HOST}" pattern="^(?:www\.)?(.+)" />
        </conditions>
        <action type="Redirect" url="https://www.{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>                
    <rule name="All HTTPS With No WWW to HTTPS+WWW" stopProcessing="true">
        <match url=".*" />
        <conditions trackAllCaptures="false">
            <add input="{SERVER_PORT_SECURE}" pattern="1" />
            <add input="{HTTP_HOST}" pattern="(?:localhost|stage\.|dev\.)" negate="true" />
            <add input="{HTTP_HOST}" pattern="^www\." negate="true" />
        </conditions>
        <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 2016-05-08
      • 2023-01-26
      • 2019-12-13
      • 1970-01-01
      • 2017-05-09
      • 2021-12-03
      • 1970-01-01
      • 2019-02-04
      相关资源
      最近更新 更多