电脑计算机论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3033|回复: 0

sitemap.xml生成方法(asp和php)

[复制链接]
udp_zerg 发表于 2010-10-18 12:47:20 | 显示全部楼层 |阅读模式
本帖最后由 udp_zerg 于 2010-10-18 17:09 编辑

sitemap.xml文件格式如下:

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9“>
<url>
<loc>http://web.it168bbs.com/<;/loc>
<lastmod>2009-04-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>http://web.it168bbs.com/index.asp<;/loc>
<lastmod>2009-04-27</lastmod>
<changefreq>weekly</changefreq>
</url>
</urlset>
那怎么制作sitemap.xml。最笨的方法就是按照这六个标签的规则,自己手写了。
如果网站的页面太多了,这个就会变成了一个超级郁闷的体力劳动。于是就有不少sitemap.xml的生成工具出现了,但是现在大部分的sitemap.xml生成工具都是在客户端输入网址,让工具在网站自行寻找链接生成,这样的模式,Rookie感觉效率比较低,而且没有办法对生成链接做控制。终于在网上找到了一个比较好的方法,适用于将内容生成静态页面的网站。有人将生成sitemap.xml的功能,写成了asp和php的页面,在页面上可以控制需要生成哪些链接。按照你的需要修改页面后,再把页面上传到你的网站空间,访问这个页面就是你所需要的sitemap.xml文件。然后就保存成为xml文件格式,再上传到你的空间,再将链接提交给支持sitemap.xml的搜索引擎。


Asp文件,将蓝色代码复制到文本文件,再保存成sitemap.asp,修改相关设置后,上传到服务器,访问即可


<%
session(”server”)=”http://web.it168bbs.com“                ‘将此[url=http://web.it168bbs.com]http://web.it168bbs.com[/url]改成你的域名
vDir = “/”                                               ‘制作SiteMap的目录
set objfso = CreateObject(”Scripting.FileSystemObject”)
root = Server.MapPath(vDir)

response.ContentType = “text/xml”
response.write “<?xml version=’1.0′ encoding=’UTF-8′?>”
response.write “<urlset xmlns=’http://www.sitemaps.org/schemas/sitemap/0.9′>”

Set objFolder = objFSO.GetFolder(root)
Set colFiles = objFolder.Files
For Each objFile In colFiles
response.write getfilelink(objFile.Path,objfile.dateLastModified)
Next
ShowSubFolders(objFolder)

response.write “</urlset>”
set fso = nothing
Sub ShowSubFolders(objFolder)
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
if folderpermission(objSubFolder.Path) then
response.write getfilelink(objSubFolder.Path,objSubFolder.dateLastModified)
Set colFiles = objSubFolder.Files
For Each objFile In colFiles
response.write getfilelink(objFile.Path,objFile.dateLastModified)
Next
ShowSubFolders(objSubFolder)
end if
Next
End Sub
Function getfilelink(file,datafile)
‘changefreq更改参数:always, hourly, daily, weekly, monthly, yearly , never
file=replace(file,root,”")
file=replace(file,”\”,”/”)
If FileExtensionIsBad(file) then Exit Function
if month(datafile)<10 then filedatem=”0″
if day(datafile)<10 then filedated=”0″
filedate=year(datafile)&”-”&filedatem&month(datafile)&”-”&filedated&day(datafile)
getfilelink = “<url><loc>”&server.htmlencode(session(”server”)&vDir&file)&”</loc><lastmod>”&filedate&”</lastmod><changefreq>weekly</changefreq></url>”
Response.Flush
End Function
Function Folderpermission(pathName)

’需要过滤的目录(不列在SiteMap里面)
PathExclusion=Array(”\ad”,”\admin”,”\aspnet_client”,”\Count”,”\data”,”\Inc”,”\upload”,”\template”)
Folderpermission =True
for each PathExcluded in PathExclusion
if instr(ucase(pathName),ucase(PathExcluded))>0 then
Folderpermission = False
exit for
end if
next
End Function
Function FileExtensionIsBad(sFileName)
Dim sFileExtension, bFileExtensionIsValid, sFileExt
Extensions = Array(”asp,html,htm”)
‘设置列表的文件名,扩展名不在其中的话SiteMap则不会收录该扩展名的文件

if len(trim(sFileName)) = 0 then
FileExtensionIsBad = true
Exit Function
end if

sFileExtension = right(sFileName, len(sFileName) - instrrev(sFileName, “.”))
bFileExtensionIsValid = false ‘assume extension is bad
for each sFileExt in extensions
if ucase(sFileExt) = ucase(sFileExtension) then
bFileExtensionIsValid = True
exit for
end if
next
FileExtensionIsBad = not bFileExtensionIsValid
End Function
%>




Php文件,将绿色代码复制到文本文件,再保存成sitemap.php,修改相关设置后,上传到服务器,访问即可

<?php
header(’Content-type: application/xml; charset=”GB2312″‘,true);
?>
<?php
$website = “http://www.grzz.com.cn“; /* 将此http://www.grzz.com.cn改成你的域名 */
$page_root = “/”; /*更改成你网站的目录地址*/

/* changefreq可自行设置 */
$changefreq = “weekly”; //”always”, “hourly”, “daily”, “weekly”, “monthly”, “yearly” and “never”.
/* 修改时间 */
$last_modification = date(”Y-m-d\TH:i:s”) . substr(date(”O”),0,3) . “:” . substr(date(”O”),3);

/* 需要生成的目录 */
$allow_dir[] = “web”;

/* 需要过滤的目录(不列在SiteMap里面) */
$disallow_dir[] = “admin”;
$disallow_dir[] = “_notes”;

/* 设置列表的文件名,扩展名不在其中的话SiteMap则不会收录该扩展名的文件 */
$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,
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.sitemaps.org/schemas/sitemap/0.9′>
<?
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>
</url>
<?
}
?>
</urlset>


您需要登录后才可以回帖 登录 | 注册

本版积分规则


QQ|手机版|小黑屋|电脑计算机论坛 ( 京ICP备2022023538号-1 )

GMT+8, 2024-5-20 07:43 , Processed in 0.073866 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表