Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用IIS将所有URL重定向到HTTPS/WWW版本_C#_Asp.net_Iis_Web Config - Fatal编程技术网

C# 使用IIS将所有URL重定向到HTTPS/WWW版本

C# 使用IIS将所有URL重定向到HTTPS/WWW版本,c#,asp.net,iis,web-config,C#,Asp.net,Iis,Web Config,我有一个网站,刚刚购买了SSL。我希望所有URL重定向到它们的HTTPS/WWW版本。例如: 1. http://mywebsite.com -> https://www.mywebsite.com 2. https://mywebsite.com -> https://www.mywebsite.com 3. http://www.mywebsite.com -> https://www.mywebsite.com 4. https://

我有一个网站,刚刚购买了SSL。我希望所有URL重定向到它们的HTTPS/WWW版本。例如:

1. http://mywebsite.com       ->   https://www.mywebsite.com
2. https://mywebsite.com      ->   https://www.mywebsite.com
3. http://www.mywebsite.com   ->   https://www.mywebsite.com
4. https://www.mywebsite.com  ->   [No redirect needed]
我已经尝试了一些解决方案:从标记为答案的方法。但对于案例2,几乎所有答案都返回“
未找到。HTTP错误404
”,即
https://mywebsite.com
未重定向到
https://www.mywebsite.com
但返回了404

已经尝试过的解决方案:

一,


二,



似乎有些人也有这个问题。但是我找不到正确的解决方案。

问题出在IIS的
站点绑定部分。没有
https://mywebsite.com
。我添加了这个记录,问题中的解决方案成功了。有关此功能的更多信息,请访问

<rule name="Redirect top domains with non-www to www" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
      <serverVariables>
        <set name="Redirect" value="false" />
      </serverVariables>
</rule>
<rule name="Force HTTPS" enabled="true" stopProcessing="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
 </rule>
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny">
      <add input="{HTTP_HOST}" pattern="^[^www]" />
      <add input="{HTTPS}" pattern="off" />
  </conditions>
  <action type="Redirect" url="https://www.zzz.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>