用PHP自动生成百度开放适配Sitemap提升移动搜索排名
随着移动端使用时长持续增长,购物、社交及信息获取行为正加速向手机迁移。手机网民规模扩大形成显著流量倒逼,迫使原忽视移动流量的网站必须关注移动搜索排名优化。
百度近期强化移动搜索体验,甚至计划取消PC网页收录。提交百度开放适配Sitemap可快速将PC搜索排名转移至移动端,避免漫长的权重重建过程。其核心是建立PC页与手机页的URL级对应关系(如`www.example.com/page` → `m.example.com/page`)。
一、准备工作
1. 移动站点部署
二、PHP自动生成代码(WordPress环境)
1. 百度开放适配Sitemap(XML格式)
```php
<?php
header('HTTP/1.1 200 OK');
$posts_to_show = 1000; // 限制生成1000篇文章
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
?>
<?php
$myposts = get_posts("numberposts=" . $posts_to_show);
foreach($myposts as $post) { ?>
<?php } ?>
```
2. 360移动适配(TXT格式)
```php
<?php
header('HTTP/1.1 200 OK');
$posts_to_show = 1000;
echo "http://yourdomain.com\thttp://m.yourdomain.com\n"; // 首页
$myposts = get_posts("numberposts=" . $posts_to_show);
foreach($myposts as $post) {

echo get_permalink() . "\t";
echo str_replace("yourdomain.com", "m.yourdomain.com", the_sitemaplink()) . "\n";
}
?>
```
3. 关键参数说明
三、WordPress主题函数补充
编辑`functions.php`,新增链接生成函数:
```php
function the_sitemaplink() {
return esc_url(apply_filters('the_permalink', get_permalink()));
}
```
四、伪静态与文件部署
1. 访问权限:若报错`500 Internal Server Error`,需设置文件权限为644
2. .htaccess规则(Apache服务器):
```apache
RewriteEngine On
RewriteRule ^(sitemap_baidu_sp)\.xml$ $1.php [L]
RewriteRule ^(sitemap_360_sp)\.txt$ $1.php [L]
```
生成静态路径:`http://yourdomain.com/sitemap_baidu_sp.xml`
五、站长平台提交
1. 百度平台
2. 360平台
六、技术限制与注意事项