【问题标题】:How to redirect http to https and www to non-www via web.config?如何通过 web.config 将 http 重定向到 https 并将 www 重定向到非 www?
【发布时间】:2017-12-01 22:24:58
【问题描述】:

我想使用 web.config 将我的 asp.net 站点上的所有请求重定向到非 www 的 https://。那就是:

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>

上面的 sn-p 负责重定向这两个:

http://
http://www

但我错过了最后一个,即:

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

怎么做?

【问题讨论】:

    标签: c# asp.net iis https web-config


    【解决方案1】:

    您需要添加第二条规则:

    <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 替换为您的域名

    【讨论】:

    • 我知道的旧帖子。我正在尝试做一个 http --> https 以及一个 www --> 非 www。 action url="http://......." 不应该是action url="https://...." 吗?既然 OP 也想要 https 重定向?
    • 在 .NET Core 3.1 中我收到 ERR_TOO_MANY_REDIRECTS 的每个客户端
    【解决方案2】:

    这是一个对我有用的通用规则:

    <rule name="Force non-WWW" enabled="true" stopProcessing="true">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAny">
            <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
        </conditions>
        <action type="Redirect" url="http://{C:2}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-24
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 2012-04-14
      • 2019-12-13
      • 1970-01-01
      相关资源
      最近更新 更多