当用户第一次访问domain.com时,会设置一个cookie并将他们重定向到domain.com/welcome(这基本上是索引,但像介绍动画一样只会显示一次)。
当用户第二次访问该网站时,它会停留在domain.com上,并且不会再访问domain.com/welcome,因为cookie已经设置了。
很简单。要测试它,请转到 http://lansana.me
问题是,我在 robots.txt 中屏蔽了/welcome,以防止搜索结果出现重复页面(因为/和/welcome 页面的内容基本相同,只是其中一个更加动画),但是当我尝试获取时在网站管理员工具中搜索 google,它说它被重定向到/welcome 页面(显然)。有什么方法可以告诉 Google 不要监听我的重定向代码吗?
这是我在 Laravel 中的 Controller ,设置 cookie 并重定向或停留在主页上 (PHP):
public function index()
{
$cookie = 'oreos';
$value = 'redirect';
$expiration = time() + (10 * 365 * 24 * 60 * 60);
$domain = '/';
if (!isset($_COOKIE[$cookie])) {
setcookie($cookie, $value, $expiration, $domain);
$articles = ArticlesModel::latest()->orderBy('created_at', 'desc')->get();
return redirect()->action('ArticlesController@welcome');
}
$articles = ArticlesModel::latest()->orderBy('created_at', 'desc')->get();
return view('pages.index', compact('articles'));
}
public function welcome()
{
$articles = ArticlesModel::latest()->orderBy('created_at', 'desc')->get();
return view('pages.welcome', compact('articles'));
}
这是基本上根据页面执行不同操作的 JavaScript(我不确定这个脚本对于这个问题有多重要):
// IGNORE THIS LINE
if ($(location).attr('pathname') == '/resume') {
.....
// if current page is /
} else if ($(location).attr('pathname') == '/') {
.....
// If current page is /welcome
} else if ($(location).attr('pathname') == '/welcome') {
.....
}
如果无法告诉 googlebot 忽略索引和欢迎方法并直接转到索引,是否有更好的方法来实现我正在做的事情,并且不会与 googlebot 发生冲突?
最佳答案
不要检查 JavaScript 中的路径,而是检查 cookie 是否存在。重定向总是不好的,因为它不利于性能,显然也不利于 Google。
您还可以将 cookie 创建移至 JavaScript(甚至为其使用不同的存储)并删除其任何服务器端逻辑。我认为这一点特别有趣,因为你的功能实际上只影响演示;服务器为什么要关心?
关于javascript - PHP cookie 设置为重定向,googlebot 也重定向 - 如何阻止这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32670024/