如何通过web.config将http重定向到https和www到非www? [英] How to redirect http to https and www to non-www via web.config?

查看:114
本文介绍了如何通过web.config将http重定向到https和www到非www?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用web.config将我的asp.net网站上的所有请求重定向到https:// with non-www。即:

I would like to use web.config to redirect all requests on my asp.net site to https:// with non-www. That is:

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

应该全部转到

https://

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

So far I have this for my 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>

上面的代码片段负责重定向这两个:

The above snippet takes care of redirecting these two:

http://
http://www

但是我错过了最后一个,这是:

But I'm missing the last, which is:

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

怎么做?

推荐答案

您需要添加第二条规则:

You need to add second rule:

<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 使用您的域名

You just need to replace sitename.com with your domain name

这篇关于如何通过web.config将http重定向到https和www到非www?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆