URL重写以删除www并使用web-config重定向到https(c#.net)

The*_*mer 7 .net c# https url-rewriting no-www

我在我的web-config中有以下代码,可以将带有前缀"www"的URL和非SSL请求重定向到https:// mydomain.com,因为SSL证书已注册到没有www的域

<rewrite>
  <rules>
    <rule name="Remove WWW prefix and redirect to https" >
      <match url="(.*)" ignoreCase="true" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" ignoreCase="true" />
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="https://mydomain.com/{R:1}" />
    </rule>
  </rules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)

这是结果:

1)HTTP:// mydomain.com/something - >的https:// mydomain.com/something(正确)

2)的http:// www.mydomain.com/something - >的https:// mydomain.com/something(正确)

3)https:// www.mydomain.com/something - >显示证书错误(此网站的安全证书存在问题.)

当您选择"继续浏览此网站(不推荐)"时.在证书错误页面上,网址被正确重写(https:// mydomain.com/something)

如何确保证书错误未显示?

谢谢

Rob*_*tas 5

解决这个问题的一种方法是注册两个单独的规则:

  1. 删除www.
  2. 强制使用 HTTPS。

    <rule name="Remove www" stopProcessing="true">
      <match url="(.*)" negate="false"></match>
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
      </conditions>
      <action type="Redirect" url="https://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    <rule name="Force HTTPS" enabled="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions>
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    
    Run Code Online (Sandbox Code Playgroud)


Mat*_*tan 0

我对此不确定,因为我在网址重写方面没有太多经验,但尝试提供帮助不会有什么坏处。
你可以试试这个

<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://mydomain.com/{R:1}" redirectType="Permanent" />
Run Code Online (Sandbox Code Playgroud)

我用谷歌搜索了很多,发现了这个,但它可能不会达到你的意图。