问题描述
在创建两个IIS URL重写规则后,我需要避免双重定向:
I need to avoid the double redirect I have after I created two IIS URL Rewrite rules to do:
1)将www重定向到非www。
1) Redirect www to non-www.
2)将HTTP重定向到HTTPS。
2) Redirect HTTP to HTTPS.
这是我的代码:
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="CanonicalHostNameRule1" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^mydomain\.com$" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="https://ABC/{R:1}" />
</rule>
(ABC是mydomain.com名称,但我必须更改它才能够发布问题)
问题在于,如果我去www它会有两个重定向,一个从www到非www,另一个来自http到https。
The problem is that if I go to www it does two redirects, one from www to non-www and a second one from http to https.
我也试过只有一个条件同时具有这两个条件但效果并不好。
I also tried having only one rules with both conditions but the result was not better.
有没有办法只进行一次重定向?
Is there a way to only do one redirect?
推荐答案
这是我使用的最终配置:
This is the final configuration I used:
<rewrite>
<rules>
<rule name="Force non-WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://yourdomainname.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
这只是一条重定向到非www和https网址的规则。
It's only one rule that redirects to non-www and https url.
这篇关于IIS将www重定向到非www,http重定向到https:是否可以只使用一个重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!