奇思妙想录 不懂得害怕的人不能算勇敢,因为勇敢指的是应对一切风云变幻坚强不屈的本事。——里欧·罗斯顿
博主

6天前在线

奇思妙想录
身体的健康因静止不动而破坏,因运动练习而长期坚持。——苏格拉底
歌曲封面 未知作品

萌ICP备20248808号

津ICP备2023004371号-2

网站已运行 3 年 131 天 5 小时 3 分

Powered by Typecho & Sunny

5 online · 51 ms

Title

谷歌站长工具提示“无法抓取站点地图”问题排查与解决(Typecho)

IhaveBB

·

技术分享

·

Article
AI摘要:Typecho博客使用Sitemap插件时,因空分类/标签输出无效时间导致谷歌无法抓取站点地图。解决方案:在插件代码中添加空值判断,跳过无内容的分类标签,修复XML格式异常。

Powered by AISummary.

最近在 谷歌站长工具(Google Search Console) 提交站点地图时遇到一个问题:提示无法抓取 sitemap。最开始以为是 URL 或 robots 配置问题,于是上网搜索了一圈,试了很多方案都没效果。

这里记录一下整个排查过程和解决方案,希望能帮到同样使用 Typecho 的朋友。

🔍 现象

在 Google Search Console 中添加 https://bbpad.com/sitemap.xml 后提示无法抓取:

🧭 排查思路

排查过的方向:
✅ URL 格式是否正确(末尾加 /、双斜线等)
✅ 服务器是否开启伪静态、是否可以访问站点文件
✅ sitemap 文件内容是否正常
✅ 站点是否开启防爬虫安全措施
结果全部没问题。
接着检查检查 sitemap 返回内容时发现了一条报错(截图如下):

🎯 定位到问题那就都好办了

Typecho 默认不自带站点地图,插件生成 sitemap,所以使用了下面的插件生成地图

Sitemap v1.0.5 - 社区维护版
https://github.com/typecho-fans/plugins/tree/master/Sitemap
根据报错定位到原因为:
某个分类或标签下没有内容,插件却读取了 modified 时间,导致输出了空时间,XML 格式异常。
可能是谷歌对此解析的比较严格,必应那边就没有这个问题。

✅ 解决方案

为避免“空分类 / 空标签”输出非法 <lastmod>,需要在插件中添加空值判断,跳过这些项。
可查看PR:https://github.com/typecho-fans/plugins/pull/201/files
简单的在输出 <lastmod> 之前判断 $art_rs['modified'] 是否为空,如果为空则 continue
打开/usr/plugins/Sitemap目录,打开Action.php文件

♾️ php 代码:
while($cates->next()){
            $art_rs = $db->fetchRow($db->select()->from('table.contents')
                    ->join('table.relationships', 'table.contents.cid = table.relationships.cid')
                    ->where('table.contents.status = ?', 'publish')
                    ->where('table.relationships.mid = ?', $cates->mid)
                    ->order('table.relationships.cid', Typecho_Db::SORT_DESC)
                    ->limit(1));
            //这里增加内容:文章的分类跳过
           if (empty($art_rs['modified'])) continue;
            echo "\t<url>\n";
            echo "\t\t<loc>".$cates->permalink."</loc>\n";
            echo "\t\t<lastmod>".date('Y-m-d',$art_rs['modified'])."</lastmod>\n";
            echo "\t\t<changefreq>daily</changefreq>\n";
            echo "\t\t<priority>0.5</priority>\n";
            echo "\t</url>\n";
        }
        foreach($tags AS $tag) {
            $type = $tag['type'];
            $art_rt = $db->fetchRow($db->select()->from('table.contents')
                    ->join('table.relationships', 'table.contents.cid = table.relationships.cid')
                    ->where('table.contents.status = ?', 'publish')
                    ->where('table.relationships.mid = ?', $tag['mid'])
                    ->order('table.relationships.cid', Typecho_Db::SORT_DESC)
                    ->limit(1));
            //这里增加内容: 文章的标签跳过
           if (empty($art_rt['modified'])) continue;
                    
            $routeExists = (NULL != Typecho_Router::get($type));
            $tag['pathinfo'] = $routeExists ? Typecho_Router::url($type, $tag) : '#';
            $tag['permalink'] = Typecho_Common::url($tag['pathinfo'], $options->index);

            echo "\t<url>\n";
            echo "\t\t<loc>".$tag['permalink']."</loc>\n";
            echo "\t\t<lastmod>".date('Y-m-d',$art_rt['modified'])."</lastmod>\n";
            echo "\t\t<changefreq>daily</changefreq>\n";
            echo "\t\t<priority>0.5</priority>\n";
            echo "\t</url>\n";
        }

修改后重新提交 sitemap,Google 即可正常抓取

😊 最后

好久没打理博客,趁这个问题顺手发了一篇文章,希望这个记录可以帮到使用typecho的同学

现在已有 18 次阅读,0 条评论,0 人点赞
Author:IhaveBB
作者
谷歌站长工具提示“无法抓取站点地图”问题排查与解决(Typecho)
当前文章累计共 3057 字,阅读大概需要 2 分钟。
国家海洋博物馆之旅
2023年7月20日 · 0评论
打造个性GitHub项目:Shields.io徽章制作
2024年7月22日 · 0评论
《相声模拟器》推荐
2024年2月7日 · 0评论
Comment:共0条
发表
搜 索 消 息 足 迹
你还不曾留言过..
你还不曾留下足迹..
博主

哈喽大家好呀

不再显示
博主