PHP cookie设置为重定向,googlebot也重定向-如何阻止这种情况


PHP cookie set to redirect, googlebot redirects too - how to stop this?

当用户第一次点击domain.com时,一个cookie被设置,他们被重定向到domain.com/welcome(这基本上是索引,但像一个介绍动画,只会显示一次)。

当用户第二次访问该站点时,它停留在domain.com上,不再转到domain.com/welcome,因为cookie已经设置了。

简单。要测试它,请转到http://lansana.me

问题是,我在robots.txt中屏蔽了/welcome以防止搜索结果中的重复页面(因为/和/welcome页面基本上具有相同的内容,一个只是更加动画),但是当我试图在网站管理员工具中获取google时,它说它被重定向到/welcome页面(显然)。我有没有办法告诉谷歌不要听我重定向的代码?

这是我的控制器在Laravel,设置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是否存在。重定向总是不好的,因为它对性能不利,显然对谷歌也是如此。

您还可以将cookie创建移动到JavaScript(甚至使用不同的存储),并删除它的任何服务器端逻辑。我认为这一点特别有趣,因为你的特性实际上只会影响演示;服务器为什么要关心呢?