【问题标题】:How to redirect all non-www URLs to https://www. in IIS?如何将所有非 www 的 URL 重定向到 https://www。在 IIS 中?
【发布时间】:2015-07-14 12:02:51
【问题描述】:

我想在 IIS 8.5 中添加正确的 301 永久重定向规则。我添加了以下规则,但它不起作用。

 <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="^(http:\/\/){0,1}(www\.){0,1}([^\.]+)\.([^\.]+)$" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://www.{C:3}.{C:4}" redirectType="Permanent" />
        </rule>

条件

总而言之,每个 URL 都应该是 HTTPS 并且应该有“www”。前缀

注意:我已经在 IIS 中安装了URL Rewrite Module

谁能帮我实现这个目标?

【问题讨论】:

    标签: asp.net redirect iis url-rewriting url-rewrite-module


    【解决方案1】:

    我通过在Web.config 文件中添加两个 URL 重写规则来管理它:

    1. 用于将非 www 重定向到 https://www.{domain}.com/...
    2. 将 HTTP 重定向到 HTTPS

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

    所有带有negate="true" 的条件都用于排除。因此,所有包含“localhost”、“stage”和“dev”的 URL 都被排除在 URL 重写之外。如果不需要,您可以删除这些条件。

    http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module了解更多关于negate属性的信息

    【讨论】:

    • 您好,您知道如何更新条件模式 (pattern="^([^\.]+)\.([^\.]+)$") 和重定向url (www.{HTTP_HOST}/{R:1}) 以便他们可以捕获和重定向域,例如 .co.uk 和 .uk.com ??
    • 嗨,我逐字复制了你的代码,但是当我输入http://example.com 时,它会重定向到https://example.com...你知道为什么它没有重定向到https://www.example.com吗?
    猜你喜欢
    • 1970-01-01
    • 2013-06-11
    • 2018-07-25
    • 2019-12-13
    • 2021-12-03
    • 2019-03-08
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    相关资源
    最近更新 更多