重写规则HTTPS除了在本地主机上时, [英] Rewrite rule to HTTPS except when on localhost

查看:164
本文介绍了重写规则HTTPS除了在本地主机上时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这里给出为基础的答案为试图重写规则添加到我的web.config文件。我希望它匹配未在本地主机上,以迫使HTTPS运行的任何URL。

I am using the answer given here as the basis for trying to add a rewrite rule to my web.config file. I want it to match any url that is not running on localhost in order to force https.

下面是我现在所拥有的:

Here is what I have right now:

<system.webServer>
  <rewrite> <!-- force https - http://stackoverflow.com/a/15119044/51 -->
    <rules>
      <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
        <match url="^((?!localhost).)*$"/>
        <conditions>
          <add input="{HTTPS}" pattern="^OFF$"/>
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
      </rule>
    </rules>
  </rewrite>
</system.webServer>

我试图使用负环视以仅匹配的URL不包括内部的本地主机网址。但是,这是行不通的。

I am trying to use a negative lookaround in order to only match url's that do not include "localhost" within the url. But this is not working.

那么如何把这个规则按顺序设置为仅重写非本地主机地址的?

So how should this rule be set up in order to only rewrite non-localhost url's?

推荐答案

尝试这种情况:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
        <match url="^(.*)$"/>
        <conditions>
          <add input="{HTTPS}" pattern="^OFF$"/>
          <add input="{HTTP_HOST}" matchType="Pattern" pattern="^localhost$" negate="true" /> 
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
      </rule>
    </rules>
  </rewrite>
</system.webServer>

使用否定本地主机模式应该做的伎俩。条件

Using a negate condition against the localhost pattern should do the trick.

这篇关于重写规则HTTPS除了在本地主机上时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆