将旧域重定向到新域保持 url
Redirect old domain to new domain keeping url
我们正在发布新版本的 Web 应用程序,并在此过程中使用新域进行品牌更改。
我们的两个应用程序都托管在 Azure 中。
当前应用程序 - 云服务
新应用程序 - Azure 网站
我想要实现的是将所有旧链接从旧域重定向到新域,同时保留 url 部分。
示例:
用户访问
https://my.currentdomain.com/any/link
并将被定向到
https://my.newdomain.io/any/link
我是否在我的 web.config 中执行从当前域到新域的 CNAME,然后 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>
您在 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。
将此代码添加到您的头文件中:
<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>
我们正在发布新版本的 Web 应用程序,并在此过程中使用新域进行品牌更改。
我们的两个应用程序都托管在 Azure 中。 当前应用程序 - 云服务 新应用程序 - Azure 网站
我想要实现的是将所有旧链接从旧域重定向到新域,同时保留 url 部分。
示例: 用户访问 https://my.currentdomain.com/any/link
并将被定向到 https://my.newdomain.io/any/link
我是否在我的 web.config 中执行从当前域到新域的 CNAME,然后 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>
您在 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。
将此代码添加到您的头文件中:
<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>