【问题标题】:HTTP to HTTPS redirect for only one URL仅针对一个 URL 的 HTTP 到 HTTPS 重定向
【发布时间】:2012-03-19 11:56:47
【问题描述】:

想知道是否有人可以提供帮助。非常感谢!

问题

  • blog.example.com
  • cool.example.com

    重定向到 https://www.example.com

目标

我们正在尝试只重定向

  • http://example.com

  • http://www.example.com

    https://www.example.com

当前代码

web.config 中的当前代码是这样的。坚持<match url="(.*)">

以下是重写规则的其余部分。

<system.webServer>     
 <rewrite>
    <rules>
       <rule name="Redirect to https">
          <match url="(.*)" />
          <conditions>
             <add input="{SERVER_PORT}" pattern="443" negate="true" />
          </conditions>
          <action type="Redirect" url="https://www.example.com" />
       </rule>
    </rules>
 </rewrite>
<modules runAllManagedModulesForAllRequests="true"/>
<defaultDocument enabled="true">
  <files>
    <clear/>    
    <!--Remove this line or place below to deprecate-->
    <add value="default.aspx"/>
    <add value="index.html"/>
    <add value="index.php"/>
    <add value="default.html"/>
  </files>
</defaultDocument>

【问题讨论】:

    标签: asp.net http redirect iis-7 https


    【解决方案1】:

    试试这个:

    <rule name="Rewrite HTTP to HTTPS" stopProcessing="false" enabled="false">
    <match url=".*" />
    <conditions>
        <add input="{HTTPS}" pattern="off" />
        <add input="{HTTP_HOST}" type=”Pattern” pattern="^(www\.)?example.com$">
    </conditions>
    <action type="Redirect" url="https://www.example.com{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
    

    【讨论】:

    • -1 - 这怎么不会为blog.example.com重定向?
    • 重点是展示一个可以按照评论中的指示进行微调的工作规则(对于捕获非 https 流量有不同的条件)。
    • 明白。没有环境就不可能证明一条成功的规则。重点是从一个松散的规则开始,并通过明显的变化(如上所述)对其进行调整以收紧它。
    • 已更新具体解决方案
    【解决方案2】:

    修改 &lt;match url="(.*)" /&gt; 以仅包含 example.comwww.example.com - 现在您正在匹配所有网址。

    http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/

    【讨论】:

    • 我对正则表达式不太熟悉。你的意思是&lt;match url="(example.com)+(www.example.com)" /&gt; 吗?
    • 您可以使用正则表达式或通配符 - 我总是使用 UI 来设置规则,这样更容易;-)
    • 对不起,您介意给我发一个 URL,我可以在那里学习如何编写此规则吗?什么是用户界面?
    • UI - 用户界面 - 您可以在 IIS 中设置规则。
    • 谢谢。我认为 UI 是一些正则表达式术语。 :p
    猜你喜欢
    • 1970-01-01
    • 2017-09-07
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多