【问题标题】:URL Rewrite rule in .NET to force www but ignore http(s).NET 中的 URL 重写规则强制 www 但忽略 http(s)
【发布时间】:2017-12-24 11:40:55
【问题描述】:

我希望在我的 web.config 文件中设置一个重写规则,强制 URL 使用“www”子域。这样做是这样的:

<rules>
    <rule name="Add WWW" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions>
       <add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
    </conditions>
    <action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />
 </rule>

但是,该项目在多个域上运行多个网站,其中一些使用 SSL,而另一些则不使用。

上面的代码将http:// 硬编码到重定向中。我要做的是抽象这一点,以便在重定向期间维护 http 或 https 协议而不会被硬编码。

期望结果的一些例子是:

非常感谢。

【问题讨论】:

    标签: url-rewriting web-config url-rewrite-module


    【解决方案1】:

    我找到了reference,它解释了执行此操作的一种方法。它使用重写键/值对作为操作 url 中的“变量”,因为(据我所知)目前无法将协议作为规则内的本机变量来获取。

    规则已修改如下,应该可以满足要求:

    <rewrite>
        <rules>
              <rule name="Add www maintain https" stopProcessing="true">
              <match url="^(.*)$" />
              <conditions>
                 <add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
              </conditions>
              <action type="Redirect" url="{MapSSL:{HTTPS}}www.{C:0}{PATH_INFO}" redirectType="Permanent" />
           </rule>
        </rules>
        <rewriteMaps>
            <rewriteMap name="MapSSL" defaultValue="http://">
                <add key="ON" value="https://" />
                <add key="OFF" value="http://" />
            </rewriteMap>
        </rewriteMaps>
    </rewrite>
    

    还有另一个reference,其中包括实现相同目标的替代方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2013-09-27
      • 2018-11-13
      • 1970-01-01
      • 2017-05-03
      • 2013-05-14
      相关资源
      最近更新 更多