2013-07-18 163 views
22

我需要将非www url重定向到http url和https url。我在web.config中尝试了以下规则。web.config将非www重定向到www

<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.comhttp://www.domain.com

请帮我纠正我的规则。

+1

第一条规则捕获这两个请求的类型,第二条规则不会被处理 – Dima

回答

40

对于适用于Match AnyMatch All两种情况的安全规则,您可以使用Rewrite Map解决方案。这是一个非常好的解决方案,唯一的缺点是需要在设置规则之前创建一个重写映射。换句话说,如果您选择将此作为您处理协议的唯一方法,那么您将是安全的。

您可以创建名为MapProtocol的重写映射,您可以在任何规则操作中使用{MapProtocol:{HTTPS}}作为协议。

<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> 

Reference

+2

该规则强制将所有链接重定向到https。我需要的是,如果它是http +非www它应该重定向到http + www 和https +非www应该重定向到https + www – Anand

+2

这一个完美工作。非常感谢 – Anand

+5

值得注意的是,您需要URL重写模块2.0([http://www.iis.net/downloads/microsoft/url-rewrite](http://www.iis.net/downloads/microsoft/url -rewrite))为此工作。 –

1

我知道这是旧的文章,但它没有完全回答。我结束了延长Satpals answer

首先映入规则HTTP + www和第二个捕获非www

出于某种原因,我不能在拳规则使用MapProtocol但与直接写入URL HTTPS工作。

<rewrite> 
    <rules> 
    <rule name="Special case www &amp; HTTPS off" enabled="true" stopProcessing="true"> 
     <match url="(.*)" /> 
     <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
     <add input="{HTTP_HOST}" pattern="^www\.example\.com$" /> 
     <add input="{HTTPS}" pattern="off" /> 
     </conditions> 
     <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="SeeOther" /> 
    </rule> 
    <rule name="Redirect to www &amp; HTTPS" enabled="true" stopProcessing="true"> 
     <match url="(.*)" /> 
     <conditions trackAllCaptures="false"> 
     <add input="{HTTP_HOST}" pattern="^example\.com$" /> 
     </conditions> 
     <action type="Redirect" url="{MapProtocol:{HTTPS}}://www.example.com/{R:1}" redirectType="SeeOther" /> 
    </rule> 
    </rules> 
    <rewriteMaps> 
    <rewriteMap name="MapProtocol"> 
     <add key="on" value="https" /> 
     <add key="off" value="http" /> 
    </rewriteMap> 
    </rewriteMaps> 
</rewrite> 
+0

这对我工作! –

3

其他的答案这里是不必要的长,这是我使用规则(只是复制和粘贴):

<rule name="ensurewww" stopProcessing="true"> 
    <match url=".*" /> 
    <conditions> 
    <add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" /> 
    </conditions> 
    <action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" /> 
</rule> 

同时保留HTTP这将重定向到www的版本和HTTPS协议

希望这有助于。