Redirect old domain to new domain keeping url

2019-06-07 14:43发布

问题:

We are releasing a new version of our Web app and in the process doing a branding change with a new domain also.

Both our apps are hosted in Azure. Current App - Cloud Service New App - Azure Website

What I want to achieve is redirect any old links from the old domain to the new domain while keeping the url portion.

Example: User visits https://my.currentdomain.com/any/link

and will be directed to https://my.newdomain.io/any/link

Do I do a CNAME from currentdomain to newdomain and then a URL Rewrite in my web.config??

Thanks!

Update - I've test this locally and it does what I'm looking for. I'll just point the cname of the old domain to the new domain and this redirect should pick up the links.

<rule name="Redirect to new domain" stopProcessing="true">
      <match url="^(.*)$" />
      <conditions>            
        <add input="{HTTP_HOST}" matchType="Pattern" pattern="^olddomain(:\d+)?$" />
      </conditions>
      <action type="Redirect" url="https://my.newdomain.io/{R:1}" redirectType="Permanent" />
    </rule>

回答1:

The fact that you host your application in Azure has minimal to no impact on the solution you are going to find.

When you do such things, you usually want to also keep all the credits that you have in the search engines. And you can only achieve that if your https://my.current.domain.com/any/link does a HTTP 301 Redirect Permanent to the new location.

Depending on the size and complexity of your project this can be a trivial task or not so easy.

One option, which is valid only if you want to redirect few (like a dozen) links. You can do that directly in your Web.Config file:

  <location path="any/link">
    <system.webServer>
      <httpRedirect enabled="true" destination="https://my.newdomain.io/any/link" httpResponseStatus="Permanent" />
    </system.webServer>
  </location>

This of course makes no sense when you have a bunch of links, or even a whole CMS behind the scenes. If this is the case, I would opt-in for writing a custom HTTP Module for IIS. This module will have the sole purpose of inspecting the incoming URI and either let it further, or generate HTTP 301 redirect.

If you are using the latest and the greatest from Microsoft, the same (custom HTTP Module) can be achieved with a custom OWIN Middleware.

If it is just domain change, all paths and query strings are to be kept, check out this good article how can you do this with URL Rewrite under IIS.