从HTTP重定向到HTTPS,并重定向到IIS7.5中具有C#MVC的新域

从HTTP重定向到HTTPS,并重定向到IIS7.5中具有C#MVC的新域,c#,asp.net-mvc,redirect,C#,Asp.net Mvc,Redirect,我在MVC项目中有一个重写规则,可以将任何HTTP请求的URL重写为HTTPS: <rewrite> <rules> <rule name="Redirect HTTP to HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true"> <match url="*" /> <conditions logicalGrouping="

我在MVC项目中有一个重写规则,可以将任何HTTP请求的URL重写为HTTPS:

<rewrite>
  <rules>
    <rule name="Redirect HTTP to HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
    </rule>
  </rules>
</rewrite>

您可以使用重写规则来处理多个域。下面是一个示例,其中包括url上的正则表达式匹配:

<rewrite>
    <globalRules>
        <rule name="Redirects to HTTPS and www.example.com" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^example.*(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^(www.)?example2.(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^www.example.net$" />
            </conditions>
            <action type="Redirect" url="https://www.example.com/{R:0}" />
        </rule>
    </globalRules>
</rewrite>


有关如何实现所需内容的更多详细信息。

重定向和url重写是完全不同的事情。您好,我的理解是,重写规则帮助我重定向请求A,对吗?不,重写实际上与重定向无关。你错了。重定向是指服务器告诉客户端“不要去那里,去其他地方”。重写是指服务器接收到X的请求,在内部说“我将把X称为Y/Z”,然后(再次在内部)服务器假装请求的URL是Y/Z。客户端根本不参与其中。打个比方,假设您是一名邮局工作人员,正在发送邮件。你看到123街的包裹。查阅你的邮局规则手册,你会看到到该地址的邮件实际上是到456街的。发件人没有得到通知。那是重写。重定向是指邮局告诉发件人“等等,他们的地址已更改”,然后将包裹返回给发件人,以便他们可以将其发送到正确的(重定向的)地址。我根本看不出您的答案在哪里使用HTTPS。我想你需要重新阅读这个问题。嗨@SlimGhost,我不明白你的答案适用于我的问题,你能帮我吗?@Patrick抱歉,我在HTTPS中省略了“S”。这不就是你想要的吗(重定向到)和http到https,也就是说(很抱歉在前面的评论中链接格式错误)Hi@SlimsGhost,并且此代码对https://old.company.com->https://new.company.com的请求有效吗?
<rewrite>
    <globalRules>
        <rule name="Redirects to HTTPS and www.example.com" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^example.*(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^(www.)?example2.(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^www.example.net$" />
            </conditions>
            <action type="Redirect" url="https://www.example.com/{R:0}" />
        </rule>
    </globalRules>
</rewrite>