【问题标题】:Redirect old domain to new domain keeping url将旧域重定向到新域保留 url
【发布时间】:2015-09-29 15:29:55
【问题描述】:

我们正在发布一个新版本的网络应用程序,并在此过程中使用新域进行品牌更改。

我们的两个应用都托管在 Azure 中。 当前应用 - 云服务 新应用 - Azure 网站

我想要实现的是将任何旧链接从旧域重定向到新域,同时保留 url 部分。

示例: 用户访问 https://my.currentdomain.com/any/link

并将被定向到 https://my.newdomain.io/any/link

我是否从 currentdomain 到 newdomain 进行 CNAME,然后在我的 web.config 中重写 URL??

谢谢!

更新 - 我已经在本地对此进行了测试,它可以满足我的需求。我只需将旧域的 cname 指向新域,此重定向应该会获取链接。

<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>

【问题讨论】:

    标签: redirect iis url-rewriting dns


    【解决方案1】:

    您在 Azure 中托管应用程序这一事实对您将要找到的解决方案影响极小,甚至没有影响。

    当您执行此类操作时,您通常还希望保留您在搜索引擎中的所有积分。只有当您的 https://my.current.domain.com/any/link 对新位置执行 HTTP 301 Redirect Permanent 时,您才能实现这一目标。

    根据您项目的规模和复杂性,这可能是一项微不足道的任务,也可能不是那么容易。

    一个选项,仅当您想要重定向几个(如十几个)链接时才有效。您可以直接在您的 Web.Config 文件中执行此操作:

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

    当您有一堆链接,甚至是幕后的整个 CMS 时,这当然没有意义。如果是这种情况,我会选择加入custom HTTP Module for IIS。该模块的唯一目的是检查传入的 URI,或者让它进一步,或者生成 HTTP 301 重定向。

    如果您使用的是 Microsoft 的最新和最强大的,则可以使用 custom OWIN Middleware 实现相同的(自定义 HTTP 模块)。

    如果只是域名变更,所有路径和查询字符串都要保留,查看this good article how can you do this with URL Rewrite under IIS。

    【讨论】:

    • 感谢您的回复。有100个链接。示例 /Feed/{Variable} 和其他类似的 Controller/Action 类型。有没有办法将 olddomain.com/*/* 重定向到 newdomain.com/*/* ?
    • 谢谢 - 我在文章中使用了该模式重定向,所有链接都重定向到新域并使用 /{R:1} 保持查询
    【解决方案2】:

    将此代码添加到您的头文件中:

    <script type="text/javascript">
        // Get Current URL
        var url = window.location.href;
        // Replace domain in URL
        var newurl = url.replace("olddomain.com", "newdomain.com");
        // Redirect to new URL
        document.location = newurl;
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 2014-10-21
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多