手把手教你制作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标签
- changefreq:页面内容更新频率。
- lastmod:页面最后修改时间
- loc:页面永久链接地址
- priority:相对于其他页面的优先权
- url:相对于前4个标签的父标签
- urlset:相对于前5个标签的父标签
我将一句一句分解讲解这个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(和) | & | & | & |
| 单引号 | ' | ' | ' |
| 双引号 | " | " | " |
| 大于号 | > | > | > |
| 小于号 | < | < | < |
4. <lastmod>2005-06-03T04:20:32-08:00</lastmod>
<lastmod>是用来指定该链接的最后更新时间,这个很重要。Google的机器人会在索引此链接前先和上次索引记录的最后更新时间进行 比较,如果时间一样就会跳过 不再索引。所以如果你的链接内容基于上次Google索引时的内容有所改变,应该更新该时间,让Google下次索引时会重新 对该链接内容进行分析和提取关键字。这里 必须用ISO 8601中指定的时间格式进行描述,格式化的时间格式如下: - 年:YYYY(2005)
- 年和月:YYYY-MM(2005-06)
- 年月日:YYYY-MM-DD(2005-06-04)
- 年月日小时分钟:YYYY-MM-DDThh:mmTZD(2005-06-04T10:37+08:00)
- 年月日小时分钟秒:YYYY-MM-DDThh:mmTZD(2005-06-04T10:37:30+08:00)
这里需注意的是TZD,TZD指定就是本地时间区域标记,像中国就是+08:00了
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程序及详细生成后代码
- <%@ CODEPAGE=65001 %>
- <% Option Explicit %>
- <% On Error Resume Next %>
- <% Response.Charset="UTF-8" %>
- <% Response.Buffer=True %>
- <!-- #include file="c_option.asp" -->
- <!-- #include file="c_function.asp" -->
- <!-- #include file="c_system_lib.asp" -->
- <!-- #include file="c_system_base.asp" -->
- <%
-
- If Request.ServerVariables("REQUEST_METHOD")="POST" Then
-
- Dim str, Conn, Rs, objStream
-
- Set Conn = Server.CreateObject("ADODB.Connection")
- Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & BlogPath & "/" & ZC_DATABASE_PATH
-
- str = "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbcrlf
- str = str & "<urlset xmlns=""http://www.google.com/schemas/sitemap/0.84""" & vbcrlf
- str = str & "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""" & vbcrlf
- str = str & "xsi:schemaLocation=""http://www.google.com/schemas/sitemap/0.84" & vbcrlf
- str = str & "http://www.google.com/schemas/sitemap/0.84/sitemap.xsd"">" & vbcrlf
- str = str & "<url>" & vbcrlf
- str = str & "<loc>" & ZC_BLOG_HOST & "</loc>" & vbcrlf
- str = str & "<lastmod>" & FormatDate("YYYY-MM-DD",Date()) & "</lastmod>" & vbcrlf
- str = str & "<changefreq>always</changefreq>" & vbcrlf
- str = str & "<priority>1.0</priority>" & vbcrlf
- str = str & "</url>" & vbcrlf
- Set Rs = Conn.Execute("select * from blog_Article where log_Level=4 order by log_PostTime desc")
- Do While Not Rs.Eof
- str = str & "<url>" & vbcrlf
- str = str & "<loc>" & ZC_BLOG_HOST & "post/" & Rs("log_ID") & "." & ZC_STATIC_TYPE & "</loc>" & vbcrlf
- str = str & "<lastmod>" & FormatDate("YYYY-MM-DD", Rs("log_PostTime")) & "</lastmod>" & vbcrlf
- str = str & "<changefreq>daily</changefreq>" & vbcrlf
- str = str & "<priority>0.8</priority>" & vbcrlf
- str = str & "</url>" & vbcrlf
- Rs.MoveNext
- Loop
- Rs.Close
- Set Rs = Nothing
- Set Conn = Nothing
- str = str & "</urlset>"
-
- Set objStream = Server.CreateObject("ADODB.Stream")
- With objStream
- .Type = adTypeText
- .Mode = adModeReadWrite
- .Open
- .Charset = "utf-8"
- .Position = objStream.Size
- .WriteText=str
- .SaveToFile BlogPath & "/googlesitemap.xml",adSaveCreateOverWrite
- .Close
- End With
-
- Set objStream = Nothing
- If Not Err Then
- Response.Write("<script>alert('成功生成站点地图!');history.back();</script>")
- Response.End
- End If
- End If
-
- Function FormatDate(FormatStr, CurDateTime)
- Dim sTemp,YYYY,YY,MM,DD,HH,mmm,SS
- sTemp = FormatStr
- If IsDate(CurDateTime) Then
- YYYY = Year(CurDateTime)
- YY = Mid(Year(CurDateTime),3,2)
- MM = Month(CurDateTime)
- If CInt(MM) < 10 Then MM = "0"&MM
- DD = Day(CurDateTime)
- If CInt(DD) < 10 Then DD = "0"&DD
- HH = Hour(CurDateTime)
- If CInt(HH) < 10 Then HH = "0"&DD
- mmm = Minute(CurDateTime)+1
- If CInt(mmm) < 10 Then mmm = "0"&mmm
- SS = Second(CurDateTime)
- If CInt(SS) < 10 Then SS = "0"&SS
- sTemp = Replace(Replace(Replace(Replace(Replace(Replace(Replace(sTemp,"YYYY",YYYY),"YY",YY),"MM",MM),"DD",DD),"HH",HH),"mm",mmm),"SS",SS)
- End If
- If IsDate(sTemp) Then
- FormatDate = sTemp
- Else
- FormatDate = CurDateTime
- End If
- End Function
- %>
- <form method=post action="">
- <input type="submit" value="生成站点地图">
- <?php require('wp-blog-header.php');
- header('Content-type: text/xml; charset=' . get_settings('blog_charset'), true);?>
- <?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
- <!-- generator="http://www.socialpatterns.com/"-->
- <urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
- http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
- <url>
- <loc><?php bloginfo('url') ?></loc>
- <lastmod><?php echo mysql2date('Y-m-dTH:i:s', get_lastpostmodified('GMT'), false); ?></lastmod>
- <changefreq>always</changefreq>
- <priority>1.0</priority>
- </url>
- <?php = ->get_results("SELECT * FROM ->posts WHERE post_status = 'publish' ORDER by post_modified DESC"); ?>
- <?php foreach ( as ) { ?>
- <url>
- <loc><?php echo get_permalink(->ID); ?></loc>
- <lastmod><?php echo mysql2date('Y-m-dTH:i:s', ->post_modified, false); ?></lastmod>
- <changefreq>daily</changefreq>
- <priority>0.8</priority>
- </url>
- <?php } ?>
- </urlset>
登陆Google提交你的SiteMap文件,让Google开始爬行吧

打开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也能达到同样效果