PHP cookie 设置为重定向,googlebot 也重定向 - 如何阻止它?

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

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

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

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

问题是,我在 robots.txt 中屏蔽了 /welcome 以防止搜索结果出现重复页面(因为 / 和 /welcome 页面的内容基本相同,只是一个更加生动),但是当我尝试在网站管理员工具中获取 google,它说它被重定向到 /welcome 页面(很明显)。有什么方法可以告诉 Google 不要听我的重定向代码吗?

这是我在 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 忽略索引和欢迎方法并直接转到索引,有没有更好的方法来实现我正在做的事情,不会与 google机器人?

不检查 JavaScript 中的路径,而是检查 cookie 是否存在。重定向总是不好的,因为它不利于性能,显然也不利于 Google。

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