什么是sitemap?我们来看下wikipedia的定义:
A site map (or sitemap) is a list of pages of a web site accessible to crawlers or users. It can be either a document in any form used as a planning tool for Web design, or a Web page that lists the pages on a Web site, typically organized in hierarchical fashion.
简单了说,就是一个站点的树形层级结构,站点的脑图,根据它可以遍历整个站点的角角落落。
那么,我们为什么需要它呢?因为sitemap可以提供给搜索引擎,提供给spider一个内容爬取的大道入口。搜索引擎如何知道你上线了一个站点,如何知道你发布了一篇博客,如何知道你的站点里含了哪些其他存档的文章等等。
如果你的站点有了sitemap,spider会在它的周期内很快收录你站点中的所有文章,按照权重提供到用户的搜索结果中。
以上只是说明了sitemap的好处和作用,不要误解sitemap是引擎收录文章的唯一入口。当然不是,只是说,如果你有了sitemap,符合指定搜索引擎所适配的sitemap,那么会收录地更快更好。具体可以抽空学习SEO知识,你会收获更多。
接下来介绍一下sitemap协议。
sitemap通用的做法是使用XML Schema书写的文档形式,必须包含XML tags,同时文档中包含的内容必须是被处理(Entity escaping)过的,同时文档必须符合UTF-8编码。
其中,sitemap必须满足以下条件:
- 以包含所有其他XML节点;
- 在urlset中参照协议标准定义相应的命名空间;
- 至少包含一个以父节点存在的有效的;
- 在节点中至少包含一个节点,以说明需要被爬取内容的链接;
- sitemap中仅允许包含相同域名的站点,如果是多个域名,则需要书写多个sitemap;
前面谈到了Entity escaping是什么呢?给大家一张图相信大家就知道了,简单说就是把一些特殊字符用其他的形式表示起来。如下图所示:
|————–+————–|
|Character | Escape Code|
|————–|:————–|
|Ampersand & | & amp;|
|————–|:————–|
|Single Quote ‘ | & apos;|
|————–|:————–|
|Double Quote “ | & quot;|
|————–|:————–|
|Greater Than > | & gt;|
|————–|:————–|
|Less Than < | & lt;|
|————–+————–|
以下提供一个简单的sitemap.xml示例供大家参考:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/</loc>
<lastmod>2005-01-01</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>
如果需要在同一个站点中维护不同subdomain的sitemap,那么可以参考如下做法:
http://www.sitemaphost.com/sitemap-host1.xml
http://www.sitemaphost.com/sitemap-host2.xml
http://www.sitemaphost.com/sitemap-host3.xml
以下做一些关于wordpress的sitemap的介绍。
wordpress制作sitemap,最为简单的途径,就是可以借助其平台强大的plugin功能,但是不管你是否相信,wordpress的plugin开启地越多,对wp系统的运行效率的影响越大。
所以,能手工添加还是尽量走手工添加定期更新为佳,毕竟搜索引擎也不会每天日以继夜地抓取你的小站点,你也不会日以继夜分秒必争地更新着你的博客文章。所以个人小博客站点类,建议手工添加sitemap文件。
在手工添加这条道路上,Google和Baidu提供了许许多多地做法,都可以达到不错的效果。我这里只简单介绍本站博客的做法。
0.在wp根目录下新增sitemap.php文件,填充以下代码:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc></loc>
<lastmod></lastmod>
<changefreq>always</changefreq>
<priority>1.0</priority>
</url>
get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' ORDER by post_modified DESC"); ?>
<url>
<loc>ID); ?></loc>
<lastmod>post_modified, false); ?></lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
</urlset>
- 新增sitemap_gen.php,填充以下代码,用以生成XML文件:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://DOMAIN:PORT/sitemap.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
file_put_contents('./sitemap.xml', $output);
curl_close($ch); - 上传到站点根目录,浏览器执行“http://DOMAIN:PORT/sitemap_gen.php,就这样sitemap.xml就在站点根目录生成成功了。
- 剩下的步骤,就是向Google和Baidu等各大搜索引擎提交sitemap.xml访问地址。
参考资料:
- Sitemap Protocol:http://www.sitemaps.org
- Simply Making Sitemap:http://www.xml-sitemaps.com
- Google Sitemap Generator WP Plugin:http://wordpress.org/plugins/google-sitemap-generator/
- Sitemap Formats And Guidelines:https://support.google.com/webmasters/answer/183668