thinkphp框架分页优化方案

发布时间:2019-06-02 06:49:50 浏览量:1333 标签: thinkphp seo

thinkphp框架是国内知名的php开发框架,但是在分页问题上对seo并不是很优好,下面我们改进框架分页规则,让其更利于seo优化。


一、改造说明


示例网址:

http://www.demo.com/list/demo.html    第一页

http://www.demo.com/list/demo.html?page=2    第二页

http://www.demo.com/list/demo.html?page=3    第三页


原有模式如上,均采用GET方式传递参数以?page作为标记,我们将其改造成以GET方式传参以   "_"方式标记,示例网址如下:


http://www.demo.com/list/demo.html    第一页

http://www.demo.com/list/demo_1.html    第二页

http://www.demo.com/list/demo_2.html    第三页


二、改造代码

方案一:全站改造

使用方式:project\thinkphp\library\think\Paginator.php,替换

image.png

if (!empty($parameters)) {
	// new batch start
	foreach($parameters as $vv){
		if($vv > 1) {
			//非首页
			if($this->currentPage == 1){
				//
				$suffix = '.' . config('url_html_suffix'); //伪静态后缀
			}else{
				$suffix = '-' . $this->currentPage . '.' . config('url_html_suffix'); //伪静态后缀
			}
			$url    = str_replace($suffix, '-', $url);
		} else {
			//首页,只保留协议部分
			$suffix = '-' . $this->currentPage . '.' . config('url_html_suffix'); //伪静态后缀
			$url    = str_replace($suffix, '', $url);
		}
		if($vv == 1){
			$url .= '.html';
		}else{
			$url .= $vv.'.html';
		}
	}
}


方案二:前台改造

if (!empty($parameters)) {
    if(strpos('此处为后台路径',$url)){
        $url .= '?' . http_build_query($parameters, null, '&');
    }else{
        // new batch start
        foreach($parameters as $vv){
            if($vv > 1) {
                //非首页
                if($this->currentPage == 1){
                    //
                    $suffix = '.' . config('url_html_suffix'); //伪静态后缀
                }else{
                    $suffix = '-' . $this->currentPage . '.' . config('url_html_suffix'); //伪静态后缀
                }
                $url    = str_replace($suffix, '-', $url);
            } else {
                //首页,只保留协议部分
                $suffix = '-' . $this->currentPage . '.' . config('url_html_suffix'); //伪静态后缀
                $url    = str_replace($suffix, '', $url);
            }
            if($vv == 1){
                $url .= '.html';
            }else{
                $url .= $vv.'.html';
            }
        }
    }
}


三、路由改造

Route::get('index$', 'admin/Link/index'); //友情链接
Route::get('index-<page>', 'admin/Link/index'); //友情链接


四、seo标题优化

/**
 * 友情链接
 * @param int $page
 * @return \think\response\View
 */
public function index($page = 0)
{
    if ($page) {
        $title = '我是列表第' . $page . '页数';

    } else {
        $title = '我是列表';
    }
    return view('Link/index', ['title' => $title]);
}



评论
登录后才可以进行评论哦! QQ登录
验证码
评论内容