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

 sitemap.xml是一种站点地图协议,此协议文件基于早期的robots.txt文件协议,并有所升级。向搜索引擎中提交了sitemap.xml的 网站将更有利于搜索引擎网页爬行机器人的爬行索引,这样将提高索引网站内容的效率和准确度。

  一共有六个标签,changefreq:页面内容更新频率;

  lastmod:页面最后修改时间;

  loc:页面永久链接地址;

  priority:相对于其他页面的优先权(这个标签可以不使用);

  url:相对于前 4个标签的父标签;

  urlset:相对于前5个标签的父标签。

  你可以向搜索引擎提供多个Sitemap文件,但提供的每个Sitemap文件包括的网址不得超过50,000 个,并且未压缩时不能大于10MB 。

  向Google提交网站地图Sitemap: 通过网址http://www.google.com/webmasters管理提交;

  向Yahoo!提交网站地图Sitemap: 通过网址http://siteexplorer.search.yahoo.com管理提交;

  向MSN提交网站地图Sitemap: 用URL直接提交:http://api.moreover.com/ping?u=http%3A//your.domainname /sitemap.xml。这是向MSN直接提交网站地图的后门URL。注意”:”被%3A替换掉。

  向ASK提交网站地图Sitemap: 直接提交。http://submissions.ask.com/ping?sitemap=http%3A//your.domainname/sitemap.xml。注意”:”被%3A替换掉。

 

sitemap.xml文件格式如下:


<?xml version=”1.0″ encoding=”UTF-8″ ?>
<urlset xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9“>
<url>
<loc>http://www.grzz.com.cn/</loc>
<lastmod>2009-04-27</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>http://www.grzz.com.cn/index.html</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,修改相关设置后,上传到服务器,访问即可

  1. <%  
  2. session(”server”)=”http://www.grzz.com.cn“                ‘将此http://www.grzz.com.cn改成你的域名  
  3. vDir = “/”                                               ‘制作SiteMap的目录  
  4. set objfso = CreateObject(”Scripting.FileSystemObject”)  
  5. root = Server.MapPath(vDir)  
  6.   
  7. response.ContentType = “text/xml”  
  8. response.write “<?xml version=’1.0′ encoding=’UTF-8′?>”  
  9. response.write “<urlset xmlns=’http://www.sitemaps.org/schemas/sitemap/0.9′>”  
  10.   
  11. Set objFolder = objFSO.GetFolder(root)  
  12. Set colFiles = objFolder.Files  
  13. For Each objFile In colFiles  
  14. response.write getfilelink(objFile.Path,objfile.dateLastModified)  
  15. Next  
  16. ShowSubFolders(objFolder)  
  17.   
  18. response.write “</urlset>”  
  19. set fso = nothing  
  20. Sub ShowSubFolders(objFolder)  
  21. Set colFolders = objFolder.SubFolders  
  22. For Each objSubFolder In colFolders  
  23. if folderpermission(objSubFolder.Path) then  
  24. response.write getfilelink(objSubFolder.Path,objSubFolder.dateLastModified)  
  25. Set colFiles = objSubFolder.Files  
  26. For Each objFile In colFiles  
  27. response.write getfilelink(objFile.Path,objFile.dateLastModified)  
  28. Next  
  29. ShowSubFolders(objSubFolder)  
  30. end if  
  31. Next  
  32. End Sub  
  33. Function getfilelink(file,datafile)  
  34. ‘changefreq更改参数:always, hourly, daily, weekly, monthly, yearly , never  
  35. file=replace(file,root,”")  
  36. file=replace(file,”\”,”/”)  
  37. If FileExtensionIsBad(file) then Exit Function  
  38. if month(datafile)<10 then filedatem=”0″  
  39. if day(datafile)<10 then filedated=”0″  
  40. filedate=year(datafile)&”-”&filedatem&month(datafile)&”-”&filedated&day(datafile)  
  41. getfilelink = “<url><loc>”&server.htmlencode(session(”server”)&vDir&file)&”</loc><lastmod>”&filedate&”</lastmod><changefreq>weekly</changefreq></url>”  
  42. Response.Flush  
  43. End Function  
  44. Function Folderpermission(pathName)  
  45.   
  46. ’需要过滤的目录(不列在SiteMap里面)  
  47. PathExclusion=Array(”\ad”,”\admin”,”\aspnet_client”,”\Count”,”\data”,”\Inc”,”\upload”,”\template”)  
  48. Folderpermission =True  
  49. for each PathExcluded in PathExclusion  
  50. if instr(ucase(pathName),ucase(PathExcluded))>0 then  
  51. Folderpermission = False  
  52. exit for  
  53. end if  
  54. next  
  55. End Function  
  56. Function FileExtensionIsBad(sFileName)  
  57. Dim sFileExtension, bFileExtensionIsValid, sFileExt  
  58. Extensions = Array(”html”)  
  59. ‘设置列表的文件名,扩展名不在其中的话SiteMap则不会收录该扩展名的文件  
  60.   
  61. if len(trim(sFileName)) = 0 then  
  62. FileExtensionIsBad = true  
  63. Exit Function  
  64. end if  
  65.   
  66. sFileExtension = right(sFileName, len(sFileName) - instrrev(sFileName, “.”))  
  67. bFileExtensionIsValid = false ‘assume extension is bad  
  68. for each sFileExt in extensions  
  69. if ucase(sFileExt) = ucase(sFileExtension) then  
  70. bFileExtensionIsValid = True  
  71. exit for  
  72. end if  
  73. next  
  74. FileExtensionIsBad = not bFileExtensionIsValid  
  75. End Function  
  76. %>  


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

    1. <?php  
    2. header(’Content-type: application/xml; charset=”GB2312″‘,true);  
    3. ?>  
    4. <?php  
    5. $website = “http://www.grzz.com.cn“; /* 将此http://www.grzz.com.cn改成你的域名 */   
    6. $page_root = “/”; /*更改成你网站的目录地址*/  
    7.   
    8. /* changefreq可自行设置 */  
    9. $changefreq = “weekly”; //”always”, “hourly”, “daily”, “weekly”, “monthly”, “yearly” and “never”.  
    10. /* 修改时间 */  
    11. $last_modification = date(”Y-m-d\TH:i:s”) . substr(date(”O”),0,3) . “:” . substr(date(”O”),3);  
    12.   
    13. /* 需要生成的目录 */  
    14. $allow_dir[] = “web”;  
    15.   
    16. /* 需要过滤的目录(不列在SiteMap里面) */  
    17. $disallow_dir[] = “admin”;  
    18. $disallow_dir[] = “_notes”;  
    19.   
    20. /* 设置列表的文件名,扩展名不在其中的话SiteMap则不会收录该扩展名的文件 */  
    21. $disallow_file[] = “.inc”;  
    22. $disallow_file[] = “.old”;  
    23. $disallow_file[] = “.save”;  
    24. $disallow_file[] = “.txt”;  
    25. $disallow_file[] = “.js”;  
    26. $disallow_file[] = “~”;  
    27. $disallow_file[] = “.LCK”;  
    28. $disallow_file[] = “.zip”;  
    29. $disallow_file[] = “.ZIP”;  
    30. $disallow_file[] = “.CSV”;  
    31. $disallow_file[] = “.csv”;  
    32. $disallow_file[] = “.css”;  
    33. $disallow_file[] = “.class”;  
    34. $disallow_file[] = “.jar”;  
    35. $disallow_file[] = “.mno”;  
    36. $disallow_file[] = “.bak”;  
    37. $disallow_file[] = “.lck”;  
    38. $disallow_file[] = “.BAK”;  
    39.   
    40. /* simple compare function: equals */  
    41. function ar_contains($key$array) {  
    42. foreach ($array as $val) {  
    43. if ($key == $val) {  
    44. return true;  
    45. }  
    46. }  
    47. return false;  
    48. }  
    49.   
    50. /* better compare function: contains */  
    51. function fl_contains($key$array) {  
    52. foreach ($array as $val) {  
    53. $pos = strpos($key$val);  
    54. if ($pos === FALSE) continue;  
    55. return true;  
    56. }  
    57.   
    58. return false;  
    59. }  
    60.   
    61. /* this function changes a substring($old_offset) of each array element to $offset */  
    62. function changeOffset($array$old_offset$offset) {  
    63. $res = array();  
    64. foreach ($array as $val) {  
    65. $res[] = str_replace($old_offset$offset$val);  
    66. }  
    67. return $res;  
    68. }  
    69.   
    70. /* this walks recursivly through all directories starting at page_root and 
    71. adds all files that fits the filter criterias */  
    72. // taken from Lasse Dalegaard,   
    73. function getFiles($directory$directory_orig = “”, $directory_offset=”") {  
    74. global $disallow_dir$disallow_file$allow_dir;  
    75.   
    76.    if ($directory_orig == “”) $directory_orig = $directory;  
    77.   
    78.    if($dir = opendir($directory)) {  
    79. // Create an array for all files found  
    80. $tmp = Array();  
    81.   
    82.        // Add the files  
    83. while($file = readdir($dir)) {  
    84. // Make sure the file exists  
    85. if($file != “.” && $file != “..” && $file[0] != ‘.’ ) {  
    86. // If it’s a directiry, list all files within it  
    87. //echo “point1<br>”;  
    88. if(is_dir($directory . “/” . $file)) {  
    89. //echo “point2<br>”;  
    90. $disallowed_abs = fl_contains($directory.”/”.$file$disallow_dir); // handle directories with pathes  
    91. $disallowed = ar_contains($file$disallow_dir); // handle directories only without pathes  
    92. $allowed_abs = fl_contains($directory.”/”.$file$allow_dir);  
    93. $allowed = ar_contains($file$allow_dir);  
    94. if ($disallowed || $disallowed_abscontinue;  
    95. if ($allowed_abs || $allowed){  
    96. $tmp2 = changeOffset(getFiles($directory . “/” . $file$directory_orig$directory_offset), $directory_orig$directory_offset);  
    97. if(is_array($tmp2)) {  
    98. $tmp = array_merge($tmp$tmp2);  
    99. }  
    100. }  
    101. else { // files  
    102. if (fl_contains($file$disallow_file)) continue;  
    103. array_push($tmpstr_replace($directory_orig$directory_offset$directory.”/”.$file));  
    104. }  
    105. }  
    106. }  
    107.   
    108.        // Finish off the function  
    109. closedir($dir);  
    110. return $tmp;  
    111. }  
    112. }  
    113.   
    114. $a = getFiles($page_root);  
    115.   
    116.   
    117. echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>’;  
    118. ?>  
    119. <urlset xmlns=’http://www.sitemaps.org/schemas/sitemap/0.9′>  
    120. <?  
    121. foreach ($a as $file) {  
    122. ?>  
    123. <url>  
    124. <loc><? echo utf8_encode($website.$file); ?></loc>  
    125. <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>  
    126. <changefreq><? echo utf8_encode($changefreq); ?></changefreq>  
    127. </url>  
    128. <?  
    129. }  
    130. ?>  
    131. </urlset> 

转载于:https://www.cnblogs.com/wind4444/archive/2012/11/06/2756526.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值