【发布时间】:2016-09-15 13:08:10
【问题描述】:
我需要同时重写https 和non-www,同时还需要不对域进行硬编码,因为我们有很多服务器。这需要在web.config 中,而不是在IIS 中。
我读过很多文章:
- http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
- http://madskristensen.net/post/url-rewrite-and-the-www-subdomain
- how to set asp.net web.config rewrite http to https and www to non-www
https 重写有效,non-www 无效。
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<!--<add input="{CACHE_URL}" pattern="*://www.*" />-->
<!--<add input="{HTTP_HOST}" pattern="*://www.*" />-->
<add input="{HTTP_HOST}" pattern="^.*www.*" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
// i've also tried
// url="{C:2}/{R:1}"
// url="{C:1}/{C:2}"
</rule>
我在正则表达式测试器上测试了 ^.*www.* 的正则表达式,它匹配 www.testing.com 但不匹配 testing.com - 所以我认为该模式会捕获它。
我需要重定向的 URL:
- testing.com ---> https://testing.com
- www.testing.com ---> https://testing.com
- www.testing.com/xyz/ ---> https://testing.com/xyz/
【问题讨论】:
标签: asp.net regex iis url-rewriting