网址重写-Web.config-HTTPS和www [英] Url Rewrite - Web.config - HTTPS and www
问题描述
我需要能够重定向到HTTPS(当前正在运行)和www(如果它不在URL中).
I need to be able to redirect to HTTPS (currently working) AND www (if it's not in the URL already).
如果URL 没有包含www
,则可以正常工作.但是,当URL 有具有www
时,它将重定向并添加其他www
,例如https://www.www.domain.com
If the URL does NOT have the www
, then it works fine. However, when the URL DOES have www
, it redirects and adds an additional www
, such as https://www.www.domain.com
请注意,我不想对域进行硬编码,而是想使用HTTPHOST
或类似的东西.
Note that I do not want to hard code the domain, and would like to use HTTPHOST
or something equivalent.
当前的重写规则:
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
推荐答案
以该正则表达式结尾-^((?!www).)*[a-zA-Z0-9]+$
Ended up with this Regex - ^((?!www).)*[a-zA-Z0-9]+$
我还必须进入IIS,并将与模式不匹配"更改为与模式匹配"
I also had to go into IIS and change "Does not match the pattern" to "Matches the pattern"
<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="WWW Rewrite" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^((?!www).)*[a-zA-Z0-9]+$" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
这篇关于网址重写-Web.config-HTTPS和www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!