早上在研究如何禁止百度转码和百度网页缓存时,在百度站长平台看到了新消息提示,查看有如下内容:
看来俺博客的移动开放适配和移动站的建设已得到了百度的认可,我长期来的研究努力并没有白费。虽然很多人并不认可用二级域名再弄一个移动站的做法,而是更加推崇响应式网页,甚至谷歌也一直推荐响应式建站。
在我看来,只要做好 PC 站和移动站的适配工作,无论是从体验还是从 SEO 角度上看,二级域名做移动站和使用响应式的区别并不大。
最有说服力的案例就是百度搜索,百度自己都一直用的二级域名做移动站:http://m.baidu.com/,所以,用二级域名走移动站,绝对不会被百度搜索所排斥!这不,百度都给俺的移动站发邀请了,一切就清白了吧!
收到邀请后,我第一时间修改了 php 代码,并提交了针对 m.zhang.ge 的 sitemap,下面简单说下步骤:
一、php 代码
以下是摘自百度官方的移动 Sitemap 协议的帮助文件:
百度推出了移动 Sitemap 协议,用于将网址提交给移动搜索收录。百度移动 Sitemap 协议是在标准 Sitemap 协议基础上制定的,增加了<mobile:mobile/>标签,它有三种取值: <mobile:mobile/> :移动网页 <mobile:mobile type="mobile"/> :移动网页 <mobile:mobile type="autoadapt"/>:自适配网页,适用于同一网址页面,会随设备不同改变展现的情况。 无该标签表示 PC 的网页
根据以上说明,可推出如下三种可用的移动 sitemap 生成 php 脚本(适合 WordPress,其他程序仅供参考):
①、非响应式 WordPress 网站适用(适用于二级域名做移动站):
<?php require('./wp-blog-header.php'); header("Content-type: text/xml"); header('HTTP/1.1 200 OK'); $posts_to_show = 1000; echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">' ?> <!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 张戈博客(https://zhang.ge)--> <url> <loc><?php echo 'http://'.$_SERVER['HTTP_HOST']; ?></loc> <mobile:mobile type="mobile"/> <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod> <changefreq>daily</changefreq> <priority>1.0</priority> </url> <?php /* 文章页面 */ $myposts = get_posts( "numberposts=" . $posts_to_show ); foreach( $myposts as $post ) { ?> <url> <loc><?php the_permalink(); ?></loc> <mobile:mobile type="mobile"/> <lastmod><?php the_time('c') ?></lastmod> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> <?php } /* 文章循环结束 */?> <?php /* 单页面 */ $mypages = get_pages(); if(count($mypages) > 0) { foreach($mypages as $page) { ?> <url> <loc><?php echo get_page_link($page->ID); ?></loc> <mobile:mobile type="mobile"/> <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?></lastmod> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> <?php }} /* 单页面循环结束 */ ?> <?php /* 博客分类 */ $terms = get_terms('category', 'orderby=name&hide_empty=0' ); $count = count($terms); if($count > 0){ foreach ($terms as $term) { ?> <url> <loc><?php echo get_term_link($term, $term->slug); ?></loc> <mobile:mobile type="mobile"/> <changefreq>weekly</changefreq> <priority>0.8</priority> </url> <?php }} /* 分类循环结束 */?> <?php /* 标签(可选) */ $tags = get_terms("post_tag"); foreach ( $tags as $key => $tag ) { $link = get_term_link( intval($tag->term_id), "post_tag" ); if ( is_wp_error( $link ) ) return false; $tags[ $key ]->link = $link; ?> <url> <loc><?php echo $link ?></loc> <mobile:mobile type="mobile"/> <changefreq>monthly</changefreq> <priority>0.4</priority> </url> <?php } /* 标签循环结束 */ ?> </urlset>
②、响应式 WordPress 网站适用:
<?php require('./wp-blog-header.php'); header("Content-type: text/xml"); header('HTTP/1.1 200 OK'); $posts_to_show = 1000; echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">' ?> <!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 张戈博客(https://zhang.ge)--> <url> <loc><?php echo 'http://'.$_SERVER['HTTP_HOST']; ?></loc> <mobile:mobile type="mobile"/> <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod> <changefreq>daily</changefreq> <priority>1.0</priority> </url> <?php /* 文章页面 */ $myposts = get_posts( "numberposts=" . $posts_to_show ); foreach( $myposts as $post ) { ?> <url> <loc><?php the_permalink(); ?></loc> <mobile:mobile type="autoadapt"/> <lastmod><?php the_time('c') ?></lastmod> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> <?php } /* 文章循环结束 */?> <?php /* 单页面 */ $mypages = get_pages(); if(count($mypages) > 0) { foreach($mypages as $page) { ?> <url> <loc><?php echo get_page_link($page->ID); ?></loc> <mobile:mobile type="autoadapt"/> <lastmod><?php echo get_page($page->ID)->post_modified; ?></lastmod> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> <?php }} /* 单页面循环结束 */ ?> <?php /* 博客分类 */ $terms = get_terms('category', 'orderby=name&hide_empty=0' ); $count = count($terms); if($count > 0){ foreach ($terms as $term) { ?> <url> <loc><?php echo get_term_link($term, $term->slug); ?></loc> <mobile:mobile type="autoadapt"/> <changefreq>weekly</changefreq> <priority>0.8</priority> </url> <?php }} /* 分类循环结束 */?> <?php /* 标签(可选) */ $tags = get_terms("post_tag"); foreach ( $tags as $key => $tag ) { $link = get_term_link( intval($tag->term_id), "post_tag" ); if ( is_wp_error( $link ) ) return false; $tags[ $key ]->link = $link; ?> <url> <loc><?php echo $link ?></loc> <mobile:mobile type="autoadapt"/> <changefreq>monthly</changefreq> <priority>0.4</priority> </url> <?php } /* 标签循环结束 */ ?> </urlset>
③、响应式二合一做法:
如果是响应式网站,其实可以将 PC 版 sitemap 改造一下,同时兼顾百度 PC 搜索和移动搜索,代码如下:
<?php require('./wp-blog-header.php'); header("Content-type: text/xml"); header('HTTP/1.1 200 OK'); $posts_to_show = 1000; echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">' ?> <!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 张戈博客(https://zhang.ge)--> <url> <loc><?php echo 'http://'.$_SERVER['HTTP_HOST']; ?></loc> <mobile:mobile type="mobile"/> <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod> <changefreq>daily</changefreq> <priority>1.0</priority> </url> <?php $myposts = get_posts( "numberposts=" . $posts_to_show ); foreach( $myposts as $post ) { ?> <url> <loc><?php the_permalink(); ?></loc> <lastmod><?php the_time('c') ?></lastmod> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> <url> <loc><?php the_permalink(); ?></loc> <mobile:mobile type="autoadapt"/> <lastmod><?php the_time('c') ?></lastmod> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> <?php } ?> <?php /* 单页面 */ $mypages = get_pages(); if(count($mypages) > 0) { foreach($mypages as $page) { ?> <url> <loc><?php echo get_page_link($page->ID); ?></loc> <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?></lastmod> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> <url> <loc><?php echo get_page_link($page->ID); ?></loc> <mobile:mobile type="autoadapt"/> <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?></lastmod> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> <?php }} /* 单页面循环结束 */ ?> <?php /* 博客分类 */ $terms = get_terms('category', 'orderby=name&hide_empty=0' ); $count = count($terms); if($count > 0){ foreach ($terms as $term) { ?> <url> <loc><?php echo get_term_link($term, $term->slug); ?></loc> <changefreq>monthly</changefreq> <priority>0.6</priority> </url> <url> <loc><?php echo get_term_link($term, $term->slug); ?></loc> <mobile:mobile type="autoadapt"/> <changefreq>weekly</changefreq> <priority>0.8</priority> </url> <?php }} /* 分类循环结束 */?> <?php /* 标签(可选) */ $tags = get_terms("post_tag"); foreach ( $tags as $key => $tag ) { $link = get_term_link( intval($tag->term_id), "post_tag" ); if ( is_wp_error( $link ) ) return false; $tags[ $key ]->link = $link; ?> <url> <loc><?php echo $link ?></loc> <changefreq>monthly</changefreq> <priority>0.4</priority> </url> <url> <loc><?php echo $link ?></loc> <mobile:mobile type="autoadapt"/> <changefreq>monthly</changefreq> <priority>0.4</priority> </url> <?php } /* 标签循环结束 */ ?> </urlset>
请根据网站实际情况,选择合适的代码保存为 sitemap_mob.php,并上传到网站根目录。
然后在浏览器访问:http://m.zhang.ge/sitemap_mob.php 查看效果。
Ps:如果响应式网站,推荐使用二合一的 php 代码,可同时提交向百度提交 PC 和移动的数据,从而避免转码困扰。
④、福利:针对响应式网站,若还没开通百度 sitemap 权限,可制作开放适配专用的 sitemap
代码如下:
<?php require('./wp-blog-header.php'); header("Content-type: text/xml"); header('HTTP/1.1 200 OK'); $posts_to_show = 1000; //限制最大生成 1000 篇 echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">'; ?> <!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 张戈博客(https://zhang.ge)--> <url> <loc><?php echo 'http://'.$_SERVER['HTTP_HOST']; ?></loc> <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod> <changefreq>daily</changefreq> <priority>1.0</priority> <data> <display> <html5_url><?php echo 'http://'.$_SERVER['HTTP_HOST']; ?></html5_url> </display> </data> </url> <?php /* 文章页面 */ header("Content-type: text/xml"); $myposts = get_posts( "numberposts=" . $posts_to_show ); foreach( $myposts as $post ) { ?> <url> <loc><?php the_permalink(); ?></loc> <lastmod><?php the_time('c') ?></lastmod> <changefreq>monthly</changefreq> <priority>0.6</priority> <data> <display> <html5_url><?php the_permalink(); ?></html5_url> </display> </data> </url> <?php } /* 文章循环结束 */ ?> <?php /* 单页面 */ $mypages = get_pages(); if(count($mypages) > 0) { foreach($mypages as $page) { ?> <url> <loc><?php echo get_page_link($page->ID); ?></loc> <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?></lastmod> <changefreq>monthly</changefreq> <priority>0.6</priority> <data> <display> <html5_url><?php echo get_page_link($page->ID); ?></html5_url> </display> </data> </url> <?php }} /* 单页面循环结束 */ ?> <?php /* 博客分类 */ $terms = get_terms('category', 'orderby=name&hide_empty=0' ); $count = count($terms); if($count > 0){ foreach ($terms as $term) { ?> <url> <loc><?php echo get_term_link($term, $term->slug); ?></loc> <changefreq>weekly</changefreq> <priority>0.8</priority> <data> <display> <html5_url><?php echo get_term_link($term, $term->slug); ?></html5_url> </display> </data> </url> <?php }} /* 分类循环结束 */?> <?php /* 标签(可选) */ $tags = get_terms("post_tag"); foreach ( $tags as $key => $tag ) { $link = get_term_link( intval($tag->term_id), "post_tag" ); if ( is_wp_error( $link ) ) return false; $tags[ $key ]->link = $link; ?> <url> <loc><?php echo $link ?></loc> <changefreq>monthly</changefreq> <priority>0.4</priority> <data> <display> <html5_url><?php echo $link ?></html5_url> </display> </data> </url> <?php } /* 标签循环结束 */ ?> </urlset>
先根据网站的实际版式,修改代码中板式标签部分,即将<xhtml_url>标签替换成实际的网站版式,以下为三种网站版式,选择一种即可:
<!— html5 版式 --> <html5_url></html5_url> <!— wml 版式 --> <wml_url></wml_url> <!— xhtml 版式(常见版式) --> <xhtml_url></xhtml_url>
不会看版式的,请参考如下说明对比一下网站的申明:
XHTML 版式申明: <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"> WML 版式申明: <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"> HTML5 版式申明: <!DOCTYPE HTML>
修改完善后,同样将上述代码保存为 sitemap_sp.php 文件。上传到网站根目录,然后使用浏览器访问该文件确认无误后,打开http://zhanzhang.baidu.com/mobiletools/index,提交百度开放适配数据即可!所有验证过的网站,均可以提交百度开放适配数据!
当然以上代码是针对响应式写的,如果是非响应式网站,请参看张戈博客之前的文章:
二、新增伪静态
①、Nginx 做法
在原来的伪静态位置新增规则:
rewrite ^/sitemap_mob.xml$ /sitemap_mob.php last;
②、Apache 做法
在.htaccess 中新增规则:
RewriteRule ^(sitemap_mob)\.xml$ $1.php
保存后,在浏览器访问http://m.zhang.ge/sitemap_mob.xml 查看效果。
Ps:其实百度支持提交 php 地址,所以第二步只是为了看起来更像 xml 文件而已,其实可做可不做!!
三、前往提交
打开百度 sitemap 工具地址:http://zhanzhang.baidu.com/sitemap/index
选择移动域名后提交上面的 sitemap_mob.xml 地址即可:
提交完毕,至于有什么效果,就不得而知了,反正俺的博客的移动搜索本来就已经适配完善了:
算是给百度当了一次小白鼠,仅此而已。
我的移动站很web站是同样的,自适应,在m.baidu.com经常被转码,我已经禁止了转码,不知道是为什么
我下一篇文章就会说如何禁止百度转码。
你的再加上一条即可:
看来你没看到我的回复啊。。。
能留言么?可以呀,干嘛看到别人说看不到解锁条,无法留言呢?
可能是存在旧的js缓存吧,不过我后面加了版本号应该不会出现问题才对。。。
可以评论文章了啊
终于升级了!
文章也跟着升级了,支持响应式适配。
恩恩 感觉挺好的!
支持一下,你的博客流量过千了么
你觉得呢?
:!: 离破千还有一定差距。。。
这个好像有点难,个人达不到那个层次。
学习了 有空的时候去折腾折腾试试
好文章 我会常来学习的 谢谢Jager大哥分享这么多好文章
共勉~
怎么查看我是否开通了百度sitemap权限?然后我现在需要用“④、福利:针对响应式网站,若还没开通百度sitemap权限,可制作开放适配专用的sitemap”还是“②、响应式WordPress网站适用:”
②是移动sitemap,是要有全新才行
④是所有网站都可以提交的开放适配sitemap
查看sitemap权限
小戈,还是弄不明白,如上代码的添加有一个手机站点吗?实在不行我还得求助有偿服务一次。
开通了百度site app的,需要提交这个吗?另外我是响应式的站点,而且也开通了site app。也就是说有个m。开头的网站,但是我想要自己的自适应页面,如何制作sitemap_sp.php文件。
响应式为啥要弄siteapp?多此一举了
能加你QQ下吗。我的286655690 我也不想啊,因为百度移动收录一直是0.我想让百度移动收录,在我认为就是要么开个m。开头的网站。要么就制作sitemap_sp.php文件。可是我不太明白响应式的站点怎么做sitemap_sp.php文件所以建立了site app
学习了!谢谢博主的教程!为做百度适配折腾了好长时间百度终端适配都没有成功。试试博主的方法怎么样!希望成功啊!
你搞定了吗?我搞了一晚上了。。。。。还没搞定
提示字段未定义 这是怎么回事?求助
“字段mobile的前缀mobile未定义”
我的网站是响应式的,按照你的提示操作后,打开xxx。com/sitemap_mob.php提示:
This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
这个本来就很容易出问题,如果没有一点php基础,还真不好debug。自己先注意下文件编码吧。
响应式的,打开sitemap_mob.php 顶部出现错误代码
Warning: Cannot modify header information - headers already sent by (output started at /public_html/sitemap_mob.php:1) in /public_html/sitemap_mob.php on line 3
Warning: Cannot modify header information - headers already sent by (output started at /public_html/sitemap_mob.php:1) in /public_html/sitemap_mob.php on line 4
建议购买我博客的有偿服务,如果只是这个文件问题,20¥足够。
Jager兄 有个小问题反馈一下 我的是响应式 安装文章所提及办法提交了开放适配 几天之后在进度查询中看不到相关的数据 并且索引量方面移动依然是0。。 有没有什么好的办法
话说你能不能不要老是换一个昵称来留言?搞得总是要审核
这回不换了 之前的忘记了。。
上传之后一直显示适配中,但没有任何数据,问了一下,百度是这么说的。
站长平台回复:
您好,经查您提交的XML文件中,h5站与PC站是一样的,属于响应式页面,响应式页面不必要提交适配关系,若不希望被百度转码建议添加禁止转码的标记,感谢您对百度的关注和支持!
‘header("Content-type: text/xml");’这个要写在输出前面,代码第3行写了就够了,‘/* 文章页面 */’下面再写一个是多余的,而且前面有echo输出,大部分主机肯定是要报错的。“header() 函数向客户端发送原始的 HTTP 报头。即必须在任何实际的输出被发送之前调用 header() 函数”。
感谢指出,确实是多余的,这篇文章代码大部分整理自以前老文章代码,忘记比对了。
Jager老师,话说你这文章内贴代码还可以展开与合上是怎么弄的 ?
Jager老师,话说你文章里边贴的代码还能展开与合上是怎么实现的
我想问下知更鸟的begin主题需要用以上代码来适配吗
响应式可以不用做适配,在header部分加上如下申明即可:
鸟哥的begin主题还需要制作移动的sitemap吗?