Detecting a 301 Redirect using Client Side Code Only

醉酒当歌 提交于 2020-01-17 03:21:12

问题


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.


回答1:


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>



回答2:


I suppose, no:

transparently follow the redirect

w3:xhr



来源:https://stackoverflow.com/questions/22622858/detecting-a-301-redirect-using-client-side-code-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!