仅使用客户端代码检测301重定向 [英] Detecting a 301 Redirect using Client Side Code Only

查看:100
本文介绍了仅使用客户端代码检测301重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不再使用的网站,因为该产品不再开发.该站点将使用301重定向将任何访问定向到新产品站点.

我想做的是向第一个访问旧站点的人显示div,以解释产品名称的更改,但只向第一个访问旧站点的人显示.

是否可以仅使用 JavaScript?我已阅读有关使用PHP的可能解决方案的信息,但是如果可能的话,我再次需要仅使用JS的解决方案.

注意:我在网站上使用的是jQuery 2.1,所以jQuery解决方案很好.

解决方案

按原样不可能.如果返回301,浏览器将跟随它,并且不显示您可能已发送的任何HTML.

或者,您可以返回200以及以下内容:

<div>The new site is at newsite.com</div>
<script type="text/javascript">
setTimeout(function() { window.location('newsite.com'); }, 60 * 1000);
</script> 

这将显示div并重定向用户(60秒后),但不会通知搜索引擎内容已移至新站点.

您可以(某种程度上)用rel="canonical"减轻SEO的困扰,但是Google等.建议您反对它(您的情况就是301的用途,rel="canonical说:此内容有两个版本,此处仅索引一个):

<head>
  <link rel="canonical" href="newsite.com">
</head>
<body>
  <div>The new site is at newsite.com</div>
  <script type="text/javascript">
  setTimeout(function() { window.location('newsite.com'); }, 60 * 1000);
  </script>
</body>

I have a site which is no longer going to be used as the product is no longer being developed. This site will use a 301 redirect to direct any visits to the new product site.

What I would like to do is show a div to anyone who first visits the old site to explain the product name change but only to those who visit the old site first.

Is this possible using only javascript? I read about possible solutions using PHP but again I need a JS only solution, if it is possible at all.

Note: I am using jQuery 2.1 on the site so a jQuery solution is fine.

解决方案

This is not possible as-is. If you return a 301, the browser will follow it, and not display any HTML you may have sent.

Alternatively, you could return 200, along with the following:

<div>The new site is at newsite.com</div>
<script type="text/javascript">
setTimeout(function() { window.location('newsite.com'); }, 60 * 1000);
</script> 

This displays the div and redirects the user (after 60 seconds) but won't inform search engines that the content has been moved to the new site.

You can (somewhat) alleviate SEO woes with rel="canonical", but Google et al. recommend against it (your situation is what 301's were made for, rel="canonical says "There are two versions of this content, only index the one over here):

<head>
  <link rel="canonical" href="newsite.com">
</head>
<body>
  <div>The new site is at newsite.com</div>
  <script type="text/javascript">
  setTimeout(function() { window.location('newsite.com'); }, 60 * 1000);
  </script>
</body>

这篇关于仅使用客户端代码检测301重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆