[英]IIS Redirect www to non-www and http to https: is it possible to do it with only one Redirect?
在创建两个 IIS URL 重写规则后,我需要避免双重重定向:
1) 将 www 重定向到非 www。
2) 将 HTTP 重定向到 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 名称,但我必须更改它才能发布问题)
问题是,如果我 go 到 www,它会进行两次重定向,一次从 www 到非 www,第二次从 http 到 https。
我也尝试过只有一个规则同时满足两种条件,但结果并没有更好。
有没有办法只做一个重定向?
这是我使用的最终配置:
<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 url 的一项规则。
您应该能够使用 HTTP 到 HTTPS,根据您的 IIS 设置,反过来会更棘手。
<rule name="HTTP to HTTPs and www to non-www " enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{SERVER_PORT}" pattern="^80$" />
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" />
</rule>
请记住,基于您在 IIS 中构建站点的方式(绑定、站点等),这一切都会影响您的重写规则的工作方式以及您将放置它们的位置。 在应用程序级别或作为全球规则。
由于我不知道您的绑定或 IIS 与其他站点的结构如何,因此不可能 100% 准确地为您编写 URL 重写规则。 因此,您可能需要对上述语法进行一些实验。
当然。 只需删除这些规则:(无需修改)
<rule name="Redirect to HTTPS without www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Special case for HTTPS with www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^ON$" />
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.