除非在本地主机上,否则将规则重写为 HTTPS [英] Rewrite rule to HTTPS except when on localhost
问题描述
我正在使用此处给出的答案作为尝试向我的 web.config 文件添加重写规则的基础.我希望它匹配任何不在本地主机上运行的 url 以强制使用 https.
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.
这是我现在所拥有的:
<system.webServer>
<rewrite> <!-- force https - https://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>
我正在尝试使用 否定环顾,以便仅匹配不包含localhost"的 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.
那么这个规则应该如何设置才能只重写非本地主机的url?
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>
对 localhost
模式使用 negate
条件应该可以解决问题.
Using a negate
condition against the localhost
pattern should do the trick.
这篇关于除非在本地主机上,否则将规则重写为 HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!