手把手教你制作Google Sitemap(详细制作教程和协议讲解)

作者: chill   日期: 2005-08-22 16:54:41   阅读: http://www.99way.com/index.php?load=read&id=400
熬了半个通宵,写出了自己的Google SiteMap文件,在这里给出详细编写教程,愿对大家有所帮助。

Google SiteMap的作用及协议格式详解

Google SiteMap Protocol是Google自己推出的一种站点地图协议,此协议文件基于早期的robots.txt文件协议,并有所升级。在Google官方指南中指出加入了Google SiteMap文件的网站将更有利于Google网页爬行机器人的爬行索引,这样将提高索引网站内容的效率和准确度。文件协议应用了简单的XML格式,一共用到6个标签,其中关键标签包括链接地址、更新时间、更新频率和索引优先权。
Google SiteMap文件生成后格式如下:
<urlset xmlns="<a href=\"http://www.google.com/schemas/sitemap/0.84\" target=\"_blank\">http://www.google.com/schemas/sitemap/0.84</a>">
      
<url>
          
<loc><a href=\"http://www.knowsky.com</loc>\" target=\"_blank\">http://www.knowsky.com</loc></a>
          
<lastmod>2005-06-03T04:20-08:00</lastmod>
          
<changefreq>always</changefreq>
          
<priority>1.0</priority>
      
</url>
      
<url>
          
<loc><a href=\"http://www.knowsky.com/300687.html</loc>\" target=\"_blank\">http://www.knowsky.com/300687.html</loc></a>
          
<lastmod>2005-06-02T20:20:36Z</lastmod>
          
<changefreq>daily</changefreq>
          
<priority>0.8</priority>
      
</url>
 
</urlset>

XML标签
我将一句一句分解讲解这个xml文件的每一个标签:

   1. <urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
      这一行定义了此xml文件的命名空间,相当于网页文件中的<html>标签一样的作用。
   2. <url>
      这是具体某一个链接的定义入口,你所希望展示在SiteMap文件中的每一个链接都要用<url>和</url>包含在里面,这是必须的。
   3. <loc>http://duduwolf.winzheng.com</loc>
      用<loc>描述出具体的链接地址,这里需要注意的是链接地址中的一些特殊字符必须转换为XML(HTML)定义的转义字符,如下表:
字符 转义后的字符
HTML字符 字符编码
and(和) & &amp; &#38;
单引号 &apos; &apos; &#39;
双引号 " &quot; &#34;
大于号 > &gt; &#62;
小于号 < &lt; &#60;
   4. <lastmod>2005-06-03T04:20:32-08:00</lastmod>
      <lastmod>是用来指定该链接的最后更新时间,这个很重要。Google的机器人会在索引此链接前先和上次索引记录的最后更新时间进行 比较,如果时间一样就会跳过      不再索引。所以如果你的链接内容基于上次Google索引时的内容有所改变,应该更新该时间,让Google下次索引时会重新 对该链接内容进行分析和提取关键字。这里      必须用ISO 8601中指定的时间格式进行描述,格式化的时间格式如下:

   5.<changefreq>always</changefreq>
      用这个标签告诉Google此链接可能会出现的更新频率,比如首页肯定就要用always(经常),而对于很久前的链接或者不再更新内容的链接就可以用 yearly(每年)。这里可以用来描述的单词共这几个:"always", "hourly", "daily", "weekly", "monthly", "yearly",具体含义我就不用解释了吧,光看单词的意思就明白了。

   6. <priority>1.0</priority>
       <priority>是用来指定此链接相对于其他链接的优先权比值,此值定于0.0 - 1.0之间

   7. 还有</url>和</urlset>,这两个就是来关闭xml标签的,这和HTML中的</body>和</html>是一个道理

   8. 另外需要注意的是,这个xml文件必须是utf-8的编码格式,不管你是手动生成还是通过代码生成,建议最好检查一下xml文件是否是utf-8编码,最简单的方法就是用记事本打开xml然后另存为时选择编码(或转换器)为UTF-8。

z-blog系统和Wordpress系统的自动生成SiteMap程序及详细生成后代码

  1. <%@ CODEPAGE=65001 %>
  2.  <% Option Explicit %>
  3.  <% On Error Resume Next %>
  4.  <% Response.Charset="UTF-8" %>
  5.  <% Response.Buffer=True %>
  6.  <!-- #include file="c_option.asp" -->
  7.  <!-- #include file="c_function.asp" -->
  8.  <!-- #include file="c_system_lib.asp" -->
  9.  <!-- #include file="c_system_base.asp" -->
  10.  <%
  11.  
  12.  If Request.ServerVariables("REQUEST_METHOD")="POST" Then
  13.  
  14.    Dim str, Conn, Rs, objStream
  15.  
  16.    Set Conn = Server.CreateObject("ADODB.Connection")
  17.    Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & BlogPath & "/" & ZC_DATABASE_PATH
  18.  
  19.    str = "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbcrlf
  20.    str = str & "<urlset xmlns=""http://www.google.com/schemas/sitemap/0.84""" & vbcrlf
  21.    str = str & "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""" & vbcrlf
  22.    str = str & "xsi:schemaLocation=""http://www.google.com/schemas/sitemap/0.84" & vbcrlf
  23.    str = str & "http://www.google.com/schemas/sitemap/0.84/sitemap.xsd"">" & vbcrlf
  24.    str = str & "<url>" & vbcrlf
  25.    str = str & "<loc>" & ZC_BLOG_HOST & "</loc>" & vbcrlf
  26.    str = str & "<lastmod>" & FormatDate("YYYY-MM-DD",Date()) & "</lastmod>" & vbcrlf
  27.    str = str & "<changefreq>always</changefreq>" & vbcrlf
  28.    str = str & "<priority>1.0</priority>" & vbcrlf
  29.    str = str & "</url>" & vbcrlf
  30.    Set Rs = Conn.Execute("select * from blog_Article where log_Level=4 order by log_PostTime desc")
  31.    Do While Not Rs.Eof
  32.      str = str & "<url>" & vbcrlf
  33.      str = str & "<loc>" & ZC_BLOG_HOST & "post/" & Rs("log_ID") & "." & ZC_STATIC_TYPE & "</loc>" & vbcrlf
  34.      str = str & "<lastmod>" & FormatDate("YYYY-MM-DD", Rs("log_PostTime")) & "</lastmod>" & vbcrlf
  35.      str = str & "<changefreq>daily</changefreq>" & vbcrlf
  36.      str = str & "<priority>0.8</priority>" & vbcrlf
  37.      str = str & "</url>" & vbcrlf
  38.      Rs.MoveNext
  39.    Loop
  40.    Rs.Close
  41.    Set Rs = Nothing
  42.    Set Conn = Nothing
  43.    str = str & "</urlset>"
  44.  
  45.    Set objStream = Server.CreateObject("ADODB.Stream")
  46.      With objStream
  47.      .Type = adTypeText
  48.      .Mode = adModeReadWrite
  49.      .Open
  50.      .Charset = "utf-8"
  51.      .Position = objStream.Size
  52.      .WriteText=str
  53.      .SaveToFile BlogPath & "/googlesitemap.xml",adSaveCreateOverWrite
  54.      .Close
  55.      End With
  56.  
  57.    Set objStream = Nothing
  58.    If Not Err Then
  59.      Response.Write("<script>alert('成功生成站点地图!');history.back();</script>")
  60.      Response.End
  61.    End If
  62.  End If
  63.  
  64.  Function FormatDate(FormatStr, CurDateTime)
  65.    Dim sTemp,YYYY,YY,MM,DD,HH,mmm,SS
  66.    sTemp = FormatStr
  67.    If IsDate(CurDateTime) Then
  68.      YYYY = Year(CurDateTime)
  69.      YY = Mid(Year(CurDateTime),3,2)
  70.      MM = Month(CurDateTime)
  71.      If CInt(MM) < 10 Then MM = "0"&MM
  72.      DD  = Day(CurDateTime)
  73.      If CInt(DD) < 10 Then DD = "0"&DD
  74.      HH = Hour(CurDateTime)
  75.      If CInt(HH) < 10 Then HH = "0"&DD
  76.      mmm = Minute(CurDateTime)+1
  77.      If CInt(mmm) < 10 Then mmm = "0"&mmm
  78.      SS = Second(CurDateTime)
  79.      If CInt(SS) < 10 Then SS = "0"&SS
  80.      sTemp = Replace(Replace(Replace(Replace(Replace(Replace(Replace(sTemp,"YYYY",YYYY),"YY",YY),"MM",MM),"DD",DD),"HH",HH),"mm",mmm),"SS",SS)
  81.    End If
  82.    If IsDate(sTemp) Then
  83.      FormatDate = sTemp
  84.    Else
  85.      FormatDate = CurDateTime
  86.    End If
  87.  End Function
  88.  %>
  89.  <form method=post action="">
  90.  <input type="submit" value="生成站点地图">

  1. <?php require('wp-blog-header.php');
  2.  header('Content-type: text/xml; charset=' . get_settings('blog_charset'), true);?>
  3.  <?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
  4.  <!-- generator="http://www.socialpatterns.com/"-->
  5.  <urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
  6.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7.  xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
  8. http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
  9.  <url>
  10.  <loc><?php bloginfo('url') ?></loc>
  11.  <lastmod><?php echo mysql2date('Y-m-dTH:i:s', get_lastpostmodified('GMT'), false); ?></lastmod>
  12.  <changefreq>always</changefreq>
  13.  <priority>1.0</priority>
  14.  </url>
  15.  <?php  = ->get_results("SELECT * FROM ->posts WHERE post_status = 'publish' ORDER by post_modified DESC"); ?>
  16.  <?php foreach ( as ) {  ?>
  17.  <url>
  18.  <loc><?php echo get_permalink(->ID); ?></loc>
  19.  <lastmod><?php echo mysql2date('Y-m-dTH:i:s', ->post_modified, false); ?></lastmod>
  20.  <changefreq>daily</changefreq>
  21.  <priority>0.8</priority>
  22.  </url>
  23.  <?php } ?>
  24.  </urlset>

登陆Google提交你的SiteMap文件,让Google开始爬行吧

我的SiteMap状态
打开https://www.google.com/webmasters/sitemaps/链接,如果还没有注册或者登陆Google,就先用自己的帐号登陆Google,登陆后转到Your Sitemaps状态页面,可以点击那个Add a Sitemap + 跳转到提交页面进行Sitemap文件的提交。建议文件放在你的站点根目录下。给Google提交你的Sitemap URL后可以看见在列表里已存在,不过这时候还没有生效,必须过几个小时后Status栏变成OK表示正式生效,如果不是OK,可以查看Google给出的状态标示解释看看是什么原因。


UPDATE:打包下载z-blog和Wordpress系统的Sitemap生成小工具
build_google_sitemap_tools.zip

UPDATE II:从webleon得到最新消息,Google也支持普通的Rss Feed,所以你也可以直接把你的Rss Url提交给Google也能达到同样效果