理解 sitemap.xml


#其他#


对于 google 来说,sitemap的作用是:将网站中页面地址提交给搜索引擎,让其快速索引站点内容。

一般命名为sitemap.xml,放在网站根目录下。例如http://www.example.com/sitemap.xml

示例:

<?xml version="1.0" encoding="UTF-8"?>
<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>
   <url>
      <loc>http://www.example.com/test</loc>
      <lastmod>2005-01-01</lastmod>
   </url>
</urlset> 

内容必须是 utf8 编码。xml标签的值必须是 entity-escaped,即对特殊字符进行转义。

支持的 XML 标签:

标签 是否可选 描述
<urlset> 必选 根标签
<url> 必选 每个url的父标签,下面结果标签是这个标签的子标签
<loc> 必选 网址。必须以协议开头(例如 http://),必须可访问,长度必须少于2048个字符
<lastmod> 可选 网址对应内容的上一次修改时间。时间格式必须符合W3C Datetime 规定的格式。也可以简单的使用 YYYY-MM-DD 这种精确到日的格式。
<changefreq> 可选 页面内容的更新频率。可选值:always、hourly、daily、weekly、monthly、yearly、never。不一定精确,但建议接近描述的值。其中 always 表示每次访问的内容都不同。
<priority> 可选 当前页面相对于本站其他页面的优先级。取值范围是0.0~1.0,值越大,优先级越高。若未设置,则取默认值0.5。这个优先级是指爬虫爬取对应网页的优先级,不是页面的优先级,不会影响搜索引擎展示结果。会影响搜索索引的结果(用site指令查询网站的结果,例如site:www.example.com)。

字符转义

sitemap 文件必须是 UTF-8 编码的 XML 文件。对于XML文件中的值必须进行转义:

字符 转义结果
& &amp;
' &apos;
" &quot;
> &gt;
< &lt;

例如如果网址是:

http://www.example.com/demo.html&q=name

那么写在 XML 中的值就是

http://www.example.com/demo.html&amp;q=name

一个 sitemap 示例:

<?xml version="1.0" encoding="UTF-8"?>
<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>
   <url>
      <loc>http://www.example.com/catalog?item=12&amp;desc=vacation_hawaii</loc>
      <changefreq>weekly</changefreq>
   </url>
   <url>
      <loc>http://www.example.com/catalog?item=73&amp;desc=vacation_new_zealand</loc>
      <lastmod>2004-12-23</lastmod>
      <changefreq>weekly</changefreq>
   </url>
   <url>
      <loc>http://www.example.com/catalog?item=74&amp;desc=vacation_newfoundland</loc>
      <lastmod>2004-12-23T18:00:15+00:00</lastmod>
      <priority>0.3</priority>
   </url>
   <url>
      <loc>http://www.example.com/catalog?item=83&amp;desc=vacation_usa</loc>
      <lastmod>2004-11-23</lastmod>
   </url>
</urlset>

使用 sitemap 索引

如果一个网站有多个sitemap,可以使用sitemap索引文件,示例如下:

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <sitemap>
      <loc>http://www.example.com/sitemap1.xml.gz</loc>
      <lastmod>2004-10-01T18:23:17+00:00</lastmod>
   </sitemap>
   <sitemap>
      <loc>http://www.example.com/sitemap2.xml.gz</loc>
      <lastmod>2005-01-01</lastmod>
   </sitemap>
</sitemapindex>

仅支持<sitemapindex><sitemap><loc><lastmod>。其中<lastmod>是可选的。

<loc>中的sitemap地址对应的sitemap文件,可以用 gzip 压缩。

注意sitempa索引文件和sitemap文件必须在同一个站点。例如http://www.yoursite.com/sitemap_index.xml中可以包含http://www.yoursite.com/sitemap.xml,但不能包含http://www.example.com/sitemap.xml

关于 sitemap 文件的位置

sitemap.xml 中的网址必须是该文件位置的同级,或者下级。

例如 http://example.com/catalog/sitemap.xml 中 URL 必须是 http://example.com/catalog/开头。在其中添加下面两个网址都是可以的:

http://example.com/catalog/show?item=23
http://example.com/catalog/show?item=233&user=3453

但是下面3个不行:

http://example.com/image/show?item=23
http://example.com/image/show?item=233&user=3453
https://example.com/catalog/page1.html

关于 lastmod 时间格式

https://www.w3.org/TR/NOTE-datetime 说明了 lastmod支持的时间格式。建议至少精确到日。

参考

https://www.google.com/sitemaps/protocol.html

https://www.w3.org/TR/NOTE-datetime


( 本文完 )