简体   繁体   English

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

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

When a user hits domain.com for the first time, a cookie is set and they are redirected to domain.com/welcome (which is basically the index but with like an introduction animation that will only show once). 当用户首次访问domain.com时,会设置一个cookie,并将其重定向到domain.com/welcome(基本上是索引,但带有一个仅显示一次的介绍动画)。

When a user visits the site a second time, it stays on domain.com and doesn't go to domain.com/welcome anymore because the cookie has been set already. 当用户第二次访问该网站时,它停留在domain.com上,不再访问domain.com/welcome,因为已经设置了cookie。

Easy enough. 很简单。 To test it, go to http://lansana.me 要对其进行测试,请访问http://lansana.me

The problem is, I blocked /welcome in robots.txt to prevent duplicate pages on search results (because the / and /welcome pages have basically the same content, one is just more animated), but when I try to fetch as google in webmaster tools, it says it was redirected to the /welcome page (obviously). 问题是,我在robots.txt中阻止了/ welcome,以防止在搜索结果中出现重复的页面(因为/和/ welcome页面的内容基本相同,其中一个页面的动画性更高),但是当我尝试在网站站长中以Google的身份获取时工具,它表示已重定向到/ welcome页面(显然)。 Is there any way I can tell Google not to listen to the code I have that redirects? 有什么办法可以告诉Google不要听我重定向的代码?

Here is my controller in Laravel, setting the cookie and redirecting or staying on the home page (PHP): 这是我在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'));
    }

Here is the JavaScript that basically does different stuff based on the page (I'm not sure how important this script is to this question): 这是基本上基于页面执行不同工作的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') {

        .....

    }

If there's no way to tell googlebot to ignore the index and welcome method and just go to index, is there a better way to achieve what I'm doing that won't conflict with googlebot? 如果没有办法告诉googlebot忽略索引和欢迎方法而只是去索引,是否有更好的方法来实现我正在做的工作而不会与googlebot冲突?

Instead of checking the path in your JavaScript, check the existence of the cookie. 而不是检查JavaScript中的路径,而是检查cookie的存在。 The redirect is always bad because its bad for performance and apparently for Google too. 重定向总是很糟糕,因为它会降低性能,甚至对Google也是不利的。

You can also move the cookie creation to JavaScript (or even use a different storage for it) and remove any server side logic for it. 您也可以将cookie创建移动到JavaScript(或什至使用其他存储),并删除任何服务器端逻辑。 I think that this point is especially interesting because your feature is actually only affecting the presentation; 我认为这一点特别有趣,因为您的功能实际上仅影响演示。 why should the server care? 服务器为什么要关心?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM