asp.net – web.config使用规则将非www或非https重定向到https:// www

我需要使用web.config中的规则将所有非www或非https流量重定向到https:// www

http://domain.com --> https://www.domain.com
http://www.domain.com --> https://www.domain.com
https://domain.com --> https://www.domain.com

修改Web.config. Redirect all traffic to www.my… Using rules element.的规则,但无法将http://www.domain.com重定向到https://www.domain.com.

<rewrite>
        <rules>
            <clear />
            <rule name="Redirect to www subdomain">
                <match url=".*" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
                    <add input="{SERVER_PROTOCOL}" pattern="^(.*)(/.*)?$"/>
                </conditions>
                <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" redirectType="Permanent"/>
            </rule>
        </rules>
    </rewrite>

解决方法

这会将所有非www或非https重定向https://www

<rewrite>
  <rules>
    <rule name="non-www-https to www https" enabled="true" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
        <add input="{HTTPS}" pattern="on" />
        <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
    </rule>

  </rules>

</rewrite>

我在IIS express(本地主机)上添加了这一行来忽略它:

<add input="{HTTP_HOST}" pattern="localhost" negate="true" />

这样做的另一个好处是您无需在此规则中输入您的域.

你可以在这里阅读更多:
http://weblogs.asp.net/owscott/redirecting-non-www-to-domain-equivalent

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....