百度网站改版nginx子目录301跳转及wordpress全站链接规则修改

在百度搜索资源平台提交网站改版时,在URL结构发生了改变的情况下,为保证站外外链以及搜索引擎的收录的有效性,最好的办法当然时把旧链接301重定向到新链接上。

同时为了提交URL对到百度,需要获取到全站的链接,可以从数据库直接读取出来,也可以使用爬虫去爬,还可以使用wordpress内置函数获取,这里介绍另外一种办法,使用wordpress自带的wordpress REST API来获取。

所以本文分两部分:

获取wordpress全站链接

这个主要是因为百度的网站改版功能提供的正则太弱了,我的连接中有连接线-,而它提供的三种匹配都不能匹配到这个简单的字符,自己写的他还不忍,所以只能对而求其次,使用URL对来表明改版的URL对应关系了。

思路就是通过wordpress REST API(官方文档)获取到需要对应的链接,然后得到新链接,生成URL对上传到百度就可以了。

贴代码:

<?php
namespace app\controllers;

use yii\helpers\Json;
use yii\web\Controller;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

/**
 * 百度站长平台修改url规则
 * Created by PhpStorm.
 * Date: 2018/12/18
 * Time: 15:09
 */
class SpiderController extends Controller
{
    public base_url;
    publicapis;

    public function beforeAction(action)
    {this->base_url = 'https://www.abc.com/wp-json/wp/v2/';
        this->apis = [
            'posts' => [
                'file' => 'postlinks.txt',
                'url' => 'posts',
            ],
            'tags' => [
                'file' => 'taglinks.txt',
                'url' => 'tags',
            ],
            'cats' => [
                'file' => 'catlinks.txt',
                'url' => 'categories',
            ],
        ];
        return true;
    }


    /**
     * 通过wordpress json api 获取链接保存到txt中
     * @paramtype string cats|posts|tags
     */
    public function actionGetUrl(type)
    {client = new Client(['base_uri' => this->base_url]);
        file_put_contents(this->apis[type]['file'], '');
        for (i = 1; i<26;i++) {
            params = [
                'page' =>i,
                'per_page' => 50,
            ];
            try {
                response =client->request('GET', this->apis[type]['url'], ['query' => params]);content = Json::decode(response->getBody());links = [];
                foreach (content asitem) {
                    links[] =item['link'];
                    file_put_contents(this->apis[type]['file'], item['link'] . PHP_EOL, FILE_APPEND);
                }
            } catch (ClientExceptione) {
                var_dump(e->getResponse());
                exit;
            }
        }
    }

    /**
     * 将保存的链接处理为百度要求的新旧URL对,存txt文件中
     * @paramtype string cats|posts|tags
     */
    public function actionDoUrl(type)
    {
        file_put_contents('new_' .this->apis[type]['file'], '');file = fopen(this->apis[type]['file'], "r");
        while (!feof(file)) {str = trim(fgets(file));str2 = str . ' ' . str_replace('/code_post/', '/',str);
            file_put_contents('new_' . this->apis[type]['file'], str2 . PHP_EOL, FILE_APPEND);
        }
        fclose(file);
        exit();
    }

}

这样你就获取到了符合百度要求的三个txt文件,里面每行一个URL对,分别是文章,分类和标签,对于月份归档这样的可以通过正则匹配的就直接正则吧。
txt文件上传到网站改版->新旧URL对里面去。
然后我们来设置Nginx301重定向

Nginx子目录301重定向

原链接:
https://www.abc.com/code_post/index-of-fanli.html;
目标链接:
https://www.abc.com/index-of-fanli.html;
需要去掉中间的子目录code_post

方法一:return

location ~* ^/code_post/(.*) {
    return 301 https://www.abc.com/$1;
    access_log off;
}

方法二:rewrite

location ~* ^/code_post/ {
    rewrite ^/code_post/(.*)https://www.abc.com/1 permanent;
    access_log off;
}

这两种实现方式,在不同域名之间的301跳转时同样适用。保存配置文件后重启Nginx

然后就到后台将wordpress固定连接中code_post去掉吧。

到此,针对百度搜索资源平台的一次网站URL改版就完成了,百度蜘蛛会根据你提交的规则去验证规则,等规则验证完流量稳定了,你就可以把301去掉了。

百度网站改版nginx子目录301跳转及wordpress全站链接规则修改

原文链接:https://beltxman.com/2254.html,若无特殊说明本站内容为 行星带 原创,未经同意禁止转载。

百度网站改版nginx子目录301跳转及wordpress全站链接规则修改”上有 5 条评论;

    1. 你这个替换规则可以写,但是301跳转就不好弄了。
      替换规则你就先把所有链接提取,然后按照你这个规则生成新url,然后一一对应起来输出到文件,然后把这个文件传到改版里面去,至于301跳转,随缘吧。
      我想说的是既然已经用框架独立做了站,为啥回来用wordpress呢,tp做二开定制功能啥的不是更方便吗?

    1. 把你的链接和数字对应起来,出URL对txt文件,上传到百度的网站改版里面去就行了。

评论已关闭。

Scroll to top