早上在研究如何禁止百度转码和百度网页缓存时,在百度站长平台看到了新消息提示,查看有如下内容:
看来俺博客的移动开放适配和移动站的建设已得到了百度的认可,我长期来的研究努力并没有白费。虽然很多人并不认可用二级域名再弄一个移动站的做法,而是更加推崇响应式网页,甚至谷歌也一直推荐响应式建站。
在我看来,只要做好 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 地址即可:
提交完毕,至于有什么效果,就不得而知了,反正俺的博客的移动搜索本来就已经适配完善了:
算是给百度当了一次小白鼠,仅此而已。