帖子

YouRanSNS PC 端自动跳转到 H5 端

当我们生成了 H5 ,怎么让网站自动跳转呢?其实比较简单,读完这篇文章就会啦。

注意:由于 uniapp 生成的 H5 与 vue 一样,属于前后端分离,搜索引擎的爬虫很难抓取到这类页面,我们用的方法是,根据 UA 去判断是否是搜索引擎爬虫,如果是则不跳转,采用 Fresns 自带的传统移动端模板,如果不是搜索引擎爬虫则跳转到 H5 页面,但是这种方法目前并未得到 #SEO# 权威人士的肯定回答,到底属于不属于 #黑帽SEO# ,如果介意,请慎重选择。

首先,我们要知道我们移动端是应用的那个模板,如下图:

这是图片

这是图片

然后连接 FTP ,打开如下目录:

这是图片

找到移动端的模板目录,然后找到如下文件,打开,如下图:

这是图片

这是图片

然后,复制如下代码,并且进行对应的修改:

<?php
    // 如果不是爬虫则跳转 H5
    if(!is_crawler()){
        $the_host = "https://www.0313.co/h5/";
        $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; // 是否有参数

        if($request_uri == "/"){
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: '.$the_host);
        }elseif(strstr($request_uri,"/group")){
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: '.$the_host.'#/pages/group/group?gid='.basename($request_uri));
        }elseif(strstr($request_uri,"/hashtag")){
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: '.$the_host.'#/pages/hashtag/hashtag?hid='.basename($request_uri));
        }elseif(strstr($request_uri,"/post")){
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: '.$the_host.'#/pages/post/post?pid='.basename($request_uri));
        }else{
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: '.$the_host);
        }
    }

    function is_crawler() {
        $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $spiders = array(
            'Googlebot', // Google 爬虫
            'Baiduspider', // 百度爬虫
            'Yahoo! Slurp', // 雅虎爬虫
            'YodaoBot', // 有道爬虫
            'msnbot' // Bing爬虫
            // 更多爬虫关键字
        );
        foreach ($spiders as $spider) {
            $spider = strtolower($spider);
            if (strpos($userAgent, $spider) !== false) {
                return true;
            }
        }
        return false;
    }
?>

然后把修改好的代码放到 fresns.blade.php 文件中的最顶部,如下图:

这是图片

然后上传覆盖即可

https://www.0313.co/post/ll1n9Ysx

未登录无法操作

评论 2

上滑加载更多