技能教程:全自动 Sitemap 索引引擎,让 Google 秒收录你的硬核内容
网站内容多但不收录?别再手动提交链接。用 PHP 写一个自动化索引哨兵,实时告诉搜索引擎你的“兵工厂”又更新了什么。
“在互联网的世界里,没被索引的内容等于不存在。”——作为极客,我们不求爬虫来访,我们要主动命令爬虫来访。
1. 为什么需要自动 Sitemap?
你的网站是动态生成的(通过 PHP 读取 Markdown)。对于搜索引擎爬虫来说,它们很难通过复杂的路由直接抓取到你深层的文章。
我们需要提供一个标准的 sitemap.xml 文件,像一份清晰的“作战地图”,告诉爬虫哪些路径是刚更新的。
2. 核心代码:PHP 动态索引生成器
在你的网站根目录下(/sy/),创建一个文件叫 sitemap.php。它会递归扫描你的所有分类文件夹,自动提取文件名并生成 XML 规范。
<?php
// 书或术:全自动 SEO 索引引擎 (sitemap.php)
header("Content-Type: text/xml; charset=utf-8");
$base_url = "[https://iuyi.top/sy/post.php](https://iuyi.top/sy/post.php)";
$post_dir = "posts/";
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="[http://www.sitemaps.org/schemas/sitemap/0.9](http://www.sitemaps.org/schemas/sitemap/0.9)">';
// 扫描所有分类目录
$categories = array_diff(scandir($post_dir), array('..', '.'));
foreach ($categories as $cat) {
$cat_path = $post_dir . $cat;
if (is_dir($cat_path)) {
// 扫描该分类下的所有 .md 文件
$files = glob($cat_path . "/*.md");
foreach ($files as $file) {
$id = basename($file, ".md");
$last_mod = date("Y-m-d", filemtime($file));
echo '<url>';
echo ' <loc>' . $base_url . '?cat=' . $cat . '&id=' . $id . '</loc>';
echo ' <lastmod>' . $last_mod . '</lastmod>';
echo ' <changefreq>weekly</changefreq>';
echo ' <priority>0.8</priority>';
echo '</url>';
}
}
}
echo '</urlset>';
?>
3. 伪静态伪装:让 PHP 看起来像 XML
为了让搜索引擎更喜欢,我们需要在 .htaccess 文件(或者 Nginx 配置)中加一行代码,把 sitemap.xml 的请求转发给 sitemap.php:
Apache
# 将 sitemap.xml 映射到我们的 PHP 脚本
RewriteRule ^sitemap\.xml$ sitemap.php [L]
4. 自动化闭环:搜索引擎主动抓取
现在,你只需要去 Google Search Console 或百度搜索资源平台,提交这个链接:https://iuyi.top/sy/sitemap.xml。
从此以后:
你的“视频哨兵”处理完一个新视频并生成了 .md 描述。
搜索引擎爬虫定期访问 sitemap.xml。
你的 PHP 脚本实时扫描到了新文件,并展示在 XML 中。
爬虫顺着链接抓取,你的硬核内容瞬间进入全球搜索索引。
这就是“书或术”的最后一块拼图:从生产、重铸、加密,到最后的全球分发。