web.config将非www重定向到www [英] web.config redirect non-www to www
问题描述
我需要将非www网址重定向到www网址,用于http和https网址。我尝试在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
请帮我纠正我的规则。
推荐答案
更安全的规则适用于匹配任何
和全部匹配
情况,您可以使用重写映射解决方案。这是一个非常好的解决方案,唯一的缺点就是设置它所需的额外费用,因为您需要在创建规则之前创建重写映射。换句话说,如果您选择使用它作为处理协议的唯一方法,那么您将是安全的。
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屋!