代码之家  ›  专栏  ›  技术社区  ›  Sherif El Sherif

IIS将非WWW重定向到WWW以及HTTP重定向到HTTPS问题

  •  1
  • Sherif El Sherif  · 技术社区  · 7 年前

    我想在IIS 8.5中将非WWW重定向到WWW以及HTTP重定向到HTTPS。如果用户类型 http://example.com ,则, http://www.example.com https://example.com ,它们都重定向到 https://www.example.com

    这是我的网站。配置文件

        <rewrite>
      <rules>
        <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.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite
    

    请注意,在IIS中,我只有1个绑定,这就是示例。问题是:对于它重定向到的所有URL https://www.example.com 但IIS的起始页不是我的默认页。aspx,当我键入 https://www.example.com/Default.aspx 它给出404错误。

    1 回复  |  直到 7 年前
        1
  •  3
  •   TTT    3 年前

    您需要将每个主机名绑定到一个网站。任何未专门绑定到网站(或服务器IP地址)的主机名将由IIS中的默认网站(绑定)处理 * -表示未绑定到正在运行的网站的任何主机名)。

    一个很简单的设置是在IIS中创建两个网站(让我们调用它们 Application Rewrites )-一个用于托管应用程序,另一个用于将其他域重写到主域。您绑定到的任何域 Rewrite 网站将其流量重定向到 https://www.example.com

    ### 应用 网站

    • 仅绑定应用程序主机名 www.example.com 在端口443上(仅限SSL)
    • 将应用程序部署到此webroot

    ### 重写 网站

    • 在IIS中创建新网站,并为其webroot创建新文件夹。
    • 绑定 example.com 在端口443和端口80上。还绑定 www.example。com公司 仅在端口80上。

    创建web。中的配置 重写 网站webroot文件夹如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Redirect any traffic to Primary hostname" enabled="true" stopProcessing="true">
              <match url="(.*)" />
              <action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
            </rule>
          </rules>
        </rewrite>        
      </system.webServer>
    </configuration>
    

    任何访问流量 http://example.com ,则, https://example.com http://www.example.com 将重定向到 https://www.example.com (包括任何路径/查询字符串)

    注意:您可以使用默认网站作为 重写 网站,或移动通配符 * 从默认网站绑定到新网站 重写 网站-将任何域重定向到 https://www.example.com -然而,这是一种不好的做法/不建议这样做。

    先决条件: 要使用此解决方案,您需要安装 IIS URL Rewrite extension 如果尚未安装。