0

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
Patrick
  • 2,995
  • 14
  • 64
  • 125
  • 1
    A redirect and a url rewrite are *completely* different things. –  Sep 19 '17 at 19:20
  • Hi, my understanding of this is that the rewrite rules helps me redirect the requesta, am I correct? – Patrick Sep 19 '17 at 19:38
  • 1
    No, rewrites have literally *nothing* to do with redirects. You are not correct. –  Sep 19 '17 at 19:39
  • Ok, what is the main difference? – Patrick Sep 19 '17 at 19:40
  • 2
    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. –  Sep 19 '17 at 19:42
  • 2
    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. –  Sep 19 '17 at 19:45
  • Ok, thanks really it's diferent. Would You like to suggest an answer to help me? – Patrick Sep 19 '17 at 19:46
  • Possible duplicate of [How to redirect HTTP to HTTPS in MVC application (IIS7.5)](https://stackoverflow.com/questions/4945883/how-to-redirect-http-to-https-in-mvc-application-iis7-5) –  Sep 19 '17 at 20:04
  • @Amy - i understand the distinction you make, but the rule in the OP's original question does accomplish the goal of client redirection **using** a config section that happens to be called "rewrite" (or rewrite rules). It is a common thing. – SlimsGhost Sep 20 '17 at 14:58

1 Answers1

-1

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.

SlimsGhost
  • 2,849
  • 1
  • 10
  • 16
  • I don't see where your answer uses HTTPS at all. I think you need to re-read the question. –  Sep 19 '17 at 19:21
  • Hi @SlimGhost, I dont understand your answer applied to my issue, can You help me on this? – Patrick Sep 19 '17 at 19:39
  • @Patrick sorry, I left off the "S" in HTTPS. Won't this do what you are wanting (redirect http://example1.com and http://example2.com to https://example1.com)? – SlimsGhost Sep 20 '17 at 14:39
  • and http to https, that is (sorry for bad link formatting in previous comment) – SlimsGhost Sep 20 '17 at 14:48
  • Hi @SlimsGhost, and this code is valid for requests from https:/ /old.company.com -> https:/ /new.company.com ? – Patrick Sep 20 '17 at 16:56
  • @Patrick it should work, but I have not tested this particular scenario. You might have problems with SSL certs if you change what site you serve to the user, but if the cert is for a wildcard domain name (*.company.com) I think it would be ok. – SlimsGhost Sep 20 '17 at 17:40