Yaa*_*lis 33 regex asp.net-mvc web-config url-rewriting
我使用这里给出的答案作为尝试向我的web.config文件添加重写规则的基础.我希望它匹配任何未在localhost上运行的url以强制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>
Run Code Online (Sandbox Code Playgroud)
我试图使用负面的环视,以便只匹配url中不包含"localhost"的url.但这不起作用.
那么如何设置此规则才能只重写非localhost网址呢?
anu*_*ava 51
试试这个条件:
<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>
Run Code Online (Sandbox Code Playgroud)
使用negate针对localhost模式的条件应该可以解决问题.
Sam*_*ppe 45
添加到anubhava的答案,您可以使用以下2个条目替换localhost的add元素以满足localhost和127.0.0.1以及可选端口,例如localhost:59400,这是通过visual studio和IIS进行调试时的情况
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^localhost(:\d+)?$" negate="true" />
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^127\.0\.0\.1(:\d+)?$" negate="true" />
Run Code Online (Sandbox Code Playgroud)
使用原始答案,localhost:123将被重定向到https,这可能是不可取的.