利用shell脚本批量提交网站404死链给百度

 百度站长平台提供的死链提交工具,可将网站存在的死链(协议死链、404页面)进行提交,可快速删除死链,帮助网站SEO优化。在提交死链的文件中逐个手动填写死链的话太麻烦,工做中咱们提倡复杂自动化,因此本文咱们一块儿交流分享Apache服务中经过shell脚本整理网站死链,便于咱们提交。php

 如何在Apache中用shell脚本提交网站404死链如何在Apache中用shell脚本提交网站404死链

1.配置Apache记录搜索引擎html

Apache是目前网站建设最为主流的web服务,可是apache的日志文件默认是不记录百度、谷歌等各大搜索引擎的爬取程序的,因此首先须要咱们设置Apache的配置文件。linux

找到Apache的配置文件httpd.conf,在配置文件中找到下面两行:web

CustomLog "logs/access_log" common
 #CustomLog "logs/access_log" combined

 
默认采用的是common,这里咱们只须要将common这一行前面加#注释掉,而后将combined这一行前的#去掉便可。而后保存重启Apache服务。shell

注:若是你的服务器上添加了多个站点,每一个站点有单独的配置文件,则咱们只须要在相应站点的配置文件中设置CustomLog项便可,例如:apache

vim /usr/local/apache/conf/vhost/www.chanzhi.org.conf


    ServerAdmin [email protected] DocumentRoot "/data/wwwroot/www.chanzhi.org"
    ServerName www.chanzhi.org
    ServerAlias chanzhi.org
    ErrorLog "/data/wwwlogs/www.chanzhi.org_error_apache.log"
    CustomLog "/data/wwwlogs/www.chanzhi.org_apache.log" combined

    SetOutputFilter DEFLATE
    Options FollowSymLinks ExecCGI
    Require all granted
    AllowOverride All
    Order allow,deny
    Allow from all
    DirectoryIndex index.html index.php

下面是配置先后的网站日志记录格式:vim

配置前:
如何在Apache中用shell脚本提交网站404死链如何在Apache中用shell脚本提交网站404死链bash

配置后:服务器

如何在Apache中用shell脚本提交网站404死链如何在Apache中用shell脚本提交网站404死链

2.编写shell脚本ide

咱们经过shell脚本获取网站日志中指定爬虫的抓取记录,而后汇总到一个文件中,便于后期使用。代码以下,好比保存为deathlink.sh

#!/bin/bash
#初始化变量
#定义蜘蛛UA信息(默认是百度蜘蛛)
UA='+http://www.baidu.com/search/spider.html'
 
#前一天的日期(apache日志)
DATE=`date +%Y%m%d -d "1 day ago"`
 
#定义日志路径
logfile=/data/wwwlogs/www.chanzhi.org_apache.log-${DATE}.log
 
#定义死链文件存放路径
deathfile=/data/wwwroot/www.chanzhi.org/deathlink.txt
 
#定义网站访问地址
website=http://www.chanzhi.org
 
#分析日志并保存死链数据
for url in `awk -v str="${UA}" '$9=="404" && $15~str {print $7}' ${logfile}`
do
  grep -q "$url" ${deathfile} || echo ${website}${url} >>${deathfile}
done

你们在使用该脚本时,根据本身服务器状况调整下路径和字段便可,而后执行脚本,:

bash deathlink.sh

3.提交死链

执行上面脚本时候,就会在指定目录下生成包含全部获取的404页面连接的文件,每一个链接占一行。例如:

如何在Apache中用shell脚本提交网站404死链如何在Apache中用shell脚本提交网站404死链

最后在站长平台提交死链页面中,填写本身的死链文件地址便可,例如:

如何在Apache中用shell脚本提交网站404死链如何在Apache中用shell脚本提交网站404死链

百度在审核经过以后,会将已经收录的失效连接删除,以免失效页面连接对网站形成不良的影响。

相关文章
相关标签/搜索