[英]Web.config URL Rewrites - HTTPS and Non-WWW
I need to have both https
and non-www
rewrites, while also NOT HARDCODING the domain , since we have numerous servers. 我需要同时使用
https
和non-www
重写,同时也不需要HARDCODING域 ,因为我们有很多服务器。 This needs to be in the web.config
, not in IIS
. 这需要在
web.config
,而不是在IIS
。
I've read numerous articles: 我读过很多文章:
The https
rewrite works, the non-www
does not. 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>
I tested the regex for ^.*www.*
on a regex tester and it was matching www.testing.com
but not testing.com
- so I would assume the pattern would catch it. 我在正则表达式测试器上测试了正则表达式为
^.*www.*
它匹配www.testing.com
而不是testing.com
- 所以我认为该模式会捕获它。
I need the URLs to redirect from: 我需要重定向的网址:
Was my own issue - there was no DNS for the www
, therefore the redirect wouldn't resolve on it's own. 是我自己的问题 -
www
没有DNS,因此重定向不会自行解决。
Code used: 使用的代码:
<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.*" />
</conditions>
<action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.