在IIS7.5中使用C#MVC从HTTP重定向到HTTPS并重定向到新域

时间:2017-09-19 16:06:46

标签: 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="MatchAny">
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
    </rule>
  </rules>
</rewrite>

但现在我需要将原始www.company.com的所有请求重定向到新的网址www2.company.com

如何将原始HTTP保持为HTTPS?

此新重定向操作的任何Google Analytics更改?

编辑:

示例:

http://old.company.com -> https://new.company.com
https://old.company.com -> https://new.company.com
http://www.old.company.com -> https://new.company.com
https://www.old.company-com -> https://new.company.com
http://old.company.com/Account/Login -> https://new.company.com/Account/Login
https://old.company.com/Account/Login -> https://new.company.com/Account/Login

1 个答案:

答案 0 :(得分:-1)

您可以使用重写规则来处理多个域。这是一个包含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>

有关如何实现所需目标的详细信息,请参阅this article