如何将首次访问者重定向到另一个页面?

W4y*_*1nt 2 html javascript php url redirect

我是否可以检查我的网站上的访问者是否访问了我的目标网页网址?(如果没有访问它,请重定向它?)

我的问题是,我需要新访问者才能查看我的目标网页,这是他们看到的第一件事.当访问者从我的一个子站点复制/粘贴链接到其他人时,这(当然)不会发生.

我一直在寻找,但空手而归.这样的事情甚至可能吗?

use*_*574 5

您可以在页面中使用Cookie来检查用户之前是否访问过目标网页

你可以通过两行来做到这一点

您的目标网页中的第一个

document.cookie = "landingPage=visited; expires=Fri, 31 Dec 9999 23:59:59 GMT";
Run Code Online (Sandbox Code Playgroud)

所有页面中的第二行或仅在默认主页中检查用户是否访问过目标网页

if(!document.cookie.includes('landingPage=visited;')) {
  window.location.href = "http://example.com/lp.html";
}
Run Code Online (Sandbox Code Playgroud)

  • 实施此操作后,我发现了一个问题,但已解决。`if(!document.cookie.includes('landingPage = visited;')){window.location.href =“ http://example.com/lp.html”; }`实际上应该是`if(!document.cookie.includes('landingPage')){window.location.href =“ http://example.com/lp.html”; }`由于实际Cookie的名称仅为“ LandingPage” (2认同)