0
votes

I have a rewrite rule in my MVC project to rewrite URL of any HTTP request to 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>

But now I need to redirect all the requests from the original www.company.com to a new url www2.company.com

How can I do that to keep also the original HTTP to HTTPS?

Any Google Analytics change for this new redirect operation?

EDIT:

Examples:

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
A redirect and a url rewrite are completely different things.user47589
Hi, my understanding of this is that the rewrite rules helps me redirect the requesta, am I correct?Patrick
No, rewrites have literally nothing to do with redirects. You are not correct.user47589
A redirect is the server telling the client "don't go there, go to this other place". A rewrite is the server receiving a request for X, internally saying "i'll call X as Y/Z instead", and thereafter (again, internally) the server pretends the URL requested is Y/Z. The client is not involved at all.user47589
To use an analogy, say you're a post office worker delivering mail. You see a package for 123 Street St. Consulting your Post Office rulebook, you see that deliveries to that address actually go to 456 Street Rd. The sender is not notified. That is a rewrite. A redirect would be the post office telling the sender "wait, their address changed", then returning the package to the sender so they can send it to the correct (redirected) address.user47589

1 Answers

-1
votes

You can use rewrite rules to handle multiple domains. Here's an example that includes regex matching on 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>

See this article for more details on how to achieve what you need.