web.config 将非 www 重定向到 www [英] web.config redirect non-www to www
问题描述
我需要将非 www 网址重定向到 http 和 https 网址的 www 网址.我尝试遵循 web.config 中的规则.
I need to redirect non-www urls to www url for both http and https urls. I tried following rules in web.config.
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" redirectType="Permanent" />
<rule name="Redirect to WWW https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="https://www.domain.com/{R:0}" redirectType="Permanent" />
它适用于非 ssl url,但在 ssl 的情况下,它从 https://domain.com 重定向到 http://www.domain.com
It works perfectly for non-ssl url but in case of ssl it redirect from https://domain.com to http://www.domain.com
请帮我改正我的规则.
推荐答案
对于适用于 Match Any
和 Match All
情况的更安全的规则,您可以使用重写地图解决方案.这是一个非常好的解决方案,唯一的缺点是设置它需要付出一点点额外的努力,因为您需要在创建规则之前创建一个重写映射.换句话说,如果您选择将此作为处理协议的唯一方法,那么您将是安全的.
For a safer rule that works for both Match Any
and Match All
situations, you can use the Rewrite Map solution. It’s a perfectly good solution with the only drawback being the ever so slight extra effort to set it up since you need to create a rewrite map before you create the rule. In other words, if you choose to use this as your sole method of handling the protocol, you’ll be safe.
您可以创建一个名为 MapProtocol 的重写映射,您可以将 {MapProtocol:{HTTPS}}
用于任何规则操作中的协议.
You can create a Rewrite Map called MapProtocol, you can use {MapProtocol:{HTTPS}}
for the protocol within any rule action.
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect"
url="{MapProtocol:{HTTPS}}://www.domain.com/{R:1}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
这篇关于web.config 将非 www 重定向到 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!