如何创建Sitemap文件
Sitemap的格式有XML, ROR, Text, HTML等。Google 可接受多种格式的 Sitemap,但 Google 鼓励使用采用 Sitemap 协议的 Sitemap。因为这使网站所有者可以提供除网址以外的其他有关网页的信息。Google还希望网站所有者只创建一个Sitemap 文件供所有搜索引擎使用。Sitemap不论格式,均具有相同的指南。
创建Sitemap文件几种方法:
一、在sitemaps.com网站在线创建Sitemap文件
它支持创建XML, ROR, Text, HTML等格式的Sitemap文件
二、在www.sitemapbuilder.net网站在线创建Sitemap文件
用它在线创建sitemap文件也相当不错
三、
如PJblog在网站根目录下有一个sitemap.asp文件,执行这个文件自动创建本网站的Sitemap文件
五、利用代码工具自动生成Sitemap文件
制作过程说明:(L-BLOG为例)
将以下代码复制到本地存为sitemap.asp或sitemap.php,上传到你的服务器网站的根目录下,运行一下就可以在你指定的目录中(建议最好是指定为根目录)自动生成一个sitemap.xml文件了。
asp程序代码:
<%
Response.Buffer = True
With Response
.Expires = -1
.AddHeader "Pragma","no-cache"
.AddHeader "cache-ctrol","no-cache"
End With
%>
<!--#include file="include/function.asp" -->
<%
Server.ScriptTimeout=50000
Dim str,objStream
str = "<?xml version='1.0' encoding='UTF-8'?>" & vbcrlf
str = str & "<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"">" & vbcrlf
str = str & getfilelink & vbcrlf
str = str & "</urlset>" & vbcrlf
Set objStream = Server.CreateObject("ADODB.Stream")
With objStream
.Open
.Charset = "utf-8"
.Position = objStream.Size
.WriteText=str
.SaveToFile server.mappath("sitemap.xml"),2 '生成的XML文件名
.Close
End With
Set objStream = Nothing
If Not Err Then
Response.Write("<head><meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><title>已经成功生成站点地图</title></head><h4>已经成功生成站点地图。</h4><br><br><a href=http://www.coosuo.com/default.asp>返回首页</a> | <a href=sitemap.xml>查看地图</a>")
Response.End
End If
Function getfilelink()
SQL="Select L.log_ID,L.log_Title,l.log_Author,L.log_PostTime,L.log_Content,L.log_IsShow,
Dim RS
Set RS=Server.CreateObject("ADODB.RecordSet")
RS.Open SQL,Conn,1,1
IF RS.EOF AND RS.BOF Then
Response.Write("<url></url>")
Else
Do While NOT RS.EOF
getfilelink = getfilelink & "<url><loc>"&SiteURL&"/blogview.asp?logID="&RS("log_ID")&"</loc><lastmod>"&DateToStr(RS("log_PostTime"),"y-m-dTH:I:S")&"</lastmod><changefreq>daily</changefreq><priority>1.0</priority></url>"
RS.MoveNext
Loop
End IF
RS.Close
Set RS=Nothing
Conn.Close
Set Conn=Nothing
End Function
%>
PHP版代码:
header('Content-type: application/xml; charset="GB2312"',true);
?>
<!--
@author Tobias Kluge, enarion.net
@version 0.2, 2005-06-05 17:40 PT
@status working
@update Aditya Naik, so1o@so1o.net
@Licence: LGPL
editor: MildSeven@im286.com
-->
<?php
$website = "http://my.xxxxx.com"; /* change this */
$page_root = "/usr/local/psa/home/vhosts/subdomains/my/httpdocs"; /* change this */
/* maybe change this: */
$changefreq = "weekly"; //"always", "hourly", "daily", "weekly", "monthly", "yearly" and "never".
$priority = 0.8;
/* this sets the last modification date of all pages to the current date */
$last_modification = date("Y-m-d\TH:i:s") . substr(date("O"),0,3) . ":" . substr(date("O"),3);
/* list of allowed directories */
$allow_dir[] = "web";
/* list of disallowed directories */
$disallow_dir[] = "admin";
$disallow_dir[] = "_notes";
/* list of disallowed file types */
$disallow_file[] = ".inc";
$disallow_file[] = ".old";
$disallow_file[] = ".save";
$disallow_file[] = ".txt";
$disallow_file[] = ".js";
$disallow_file[] = "~";
$disallow_file[] = ".LCK";
$disallow_file[] = ".zip";
$disallow_file[] = ".ZIP";
$disallow_file[] = ".CSV";
$disallow_file[] = ".csv";
$disallow_file[] = ".css";
$disallow_file[] = ".class";
$disallow_file[] = ".jar";
$disallow_file[] = ".mno";
$disallow_file[] = ".bak";
$disallow_file[] = ".lck";
$disallow_file[] = ".BAK";
/* simple compare function: equals */
function ar_contains($key, $array) {
foreach ($array as $val) {
if ($key == $val) {
return true;
}
}
return false;
}
/* better compare function: contains */
function fl_contains($key, $array) {
foreach ($array as $val) {
$pos = strpos($key, $val);
if ($pos === FALSE) continue;
return true;
}
return false;
}
/* this function changes a substring($old_offset) of each array element to $offset */
function changeOffset($array, $old_offset, $offset) {
$res = array();
foreach ($array as $val) {
$res[] = str_replace($old_offset, $offset, $val);
}
return $res;
}
/* this walks recursivly through all directories starting at page_root and
adds all files that fits the filter criterias */
// taken from Lasse Dalegaard, http://php.net/opendir
function getFiles($directory, $directory_orig = "", $directory_offset="") {
global $disallow_dir, $disallow_file, $allow_dir;
if ($directory_orig == "") $directory_orig = $directory;
if($dir = opendir($directory)) {
// Create an array for all files found
$tmp = Array();
// Add the files
while($file = readdir($dir)) {
// Make sure the file exists
if($file != "." && $file != ".." && $file[0] != '.' ) {
// If it's a directiry, list all files within it
//echo "point1<br>";
if(is_dir($directory . "/" . $file)) {
//echo "point2<br>";
$disallowed_abs = fl_contains($directory."/".$file, $disallow_dir); // handle directories with pathes
$disallowed = ar_contains($file, $disallow_dir); // handle directories only without pathes
$allowed_abs = fl_contains($directory."/".$file, $allow_dir);
$allowed = ar_contains($file, $allow_dir);
if ($disallowed || $disallowed_abs) continue;
if ($allowed_abs || $allowed){
$tmp2 = changeOffset(getFiles($directory . "/" . $file, $directory_orig, $directory_offset), $directory_orig, $directory_offset);
if(is_array($tmp2)) {
$tmp = array_merge($tmp, $tmp2);
}
}
} else { // files
if (fl_contains($file, $disallow_file)) continue;
array_push($tmp, str_replace($directory_orig, $directory_offset, $directory."/".$file));
}
}
}
// Finish off the function
closedir($dir);
return $tmp;
}
}
$a = getFiles($page_root);
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<?
foreach ($a as $file) {
?>
<url>
<loc><? echo utf8_encode($website.$file); ?></loc>
<lastmod><? echo utf8_encode(date("Y-m-d\TH:i:s", filectime($page_root.$file)). substr(date("O"),0,3) . ":" . substr(date("O"),3));?></lastmod>
<changefreq><? echo utf8_encode($changefreq); ?></changefreq>
<priority><? echo utf8_encode($priority); ?></priority>
</url>
<?
}
?>
</urlset>
参数说明:
$website--你的域名
$page_root--你的站点的绝对路径
$changefreq--更新时间单位 "always"(始终), "hourly"(小时), "daily"(天), "weekly"(星期), "monthly"(月), "yearly" "never"(从不)
$priority--更新频率
$allow_dir--允许列表的目录
$disallow_dir--禁止列表的目录
$disallow_file--禁止列表的文件类型
最后进入google提交页面:
进Google 网站管理员工具,有gmail的可以用gmail直接登录。登录后把生成的xml文件地址按步骤提交就可以了。
注:你的网站如果增加了新的页面,可以在增加完后再运行一下sitemap.asp或sitemap.php,并再重新提交一下sitemap.xml文件,这样就可以保证sitemap.xml文件的不断更新了!
*********************
补充:
另外向雅虎提交Sitemap:在“雅虎站长工具”里的“我的网站收藏”里输入你的博客网址,点击“加入搜藏”,然后点击“开始认证”,在页面头部加入雅虎要求你在META里加入的代码,更新。完成之后点击“提交认证”即可。
雅虎认证通过后点击“Sitemap/RSS”的“管理”,输入sitemap.xml,点击“添加Sitemap/RSS”,由于雅虎的Sitemap必须是.txt格式的文件,而我们尚没有生成txt格式的Sitemap的插件,所以这里我们只能提交RSS了,输入rss/(不要忽略/),提交即可。
个人网站:生活百科
本文版权所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。