如何通过web.config将http重定向到https和www到非www?

问题描述:

我想使用web.config将我的asp.net网站上的所有请求重定向到https://与非www。那就是:如何通过web.config将http重定向到https和www到非www?

http:// 
http://www 
https://www 

都应该去

https:// 

到目前为止,我有这对我的web.config:

<system.webServer> 
... 
<rewrite> 
    <rules> 
    <clear /> 
    <rule name="Redirect to https" stopProcessing="true"> 
     <match url=".*" /> 
     <conditions> 
     <add input="{HTTPS}" pattern="off" ignoreCase="true" /> 
     </conditions> 
     <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" /> 
    </rule> 
    </rules> 
</rewrite> 
</system.webServer> 

上面的代码需要重新引导这两种护理:

http:// 
http://www 

但我失踪日e最后,这是:

https://www ---> https:// 

如何做到这一点?

您需要添加第二条规则:

<rule name="NonWwwRedirect" stopProcessing="true"> 
    <match url="(.*)" /> 
    <conditions> 
     <add input="{HTTP_HOST}" pattern="^www.sitename\.com$" /> 
    </conditions> 
    <action type="Redirect" url="http://sitename.com/{R:1}" /> 
</rule> 

你只需要使用您的域名来代替sitename.com