【发布时间】:2021-04-12 03:23:31
【问题描述】:
我已经看到了很多接近的答案,比如这个:IIS Redirect non-www to www AND http to https,但它与我的情况并不完全匹配。
我有一个网站,在其主域名之上有 2 个域别名。为每个版本显示正确的内容是在代码本身中完成的。
我尝试使用 WEB.CONFIG 文件在 2 种情况下重写:不使用 HTTPS 和/或 URL 开头没有 WWW 时。
所以http://example.com、https://example.com 和http://www.example.com 都将被重写为https://www.example.com 如果使用其域别名之一访问该站点,同样的情况,例如http://examplealias.com 将变为https://www.examplealias.com
这个答案
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
对我不起作用,因为我无法对域名进行硬编码。
我试过了
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
但是如果我打电话给http://www.example.com,这会导致https://www.www.example.com
结合这两个条件的正确方法是什么?
【问题讨论】:
-
删除重定向中的
www.。这可能会有所帮助stackoverflow.com/a/53341759/9936356
标签: iis web-config