[ASP]sitemap地圖產生代碼

來源:互聯網
上載者:User
儲存為sitemap.asp,然後放到網站根目錄下,設定要產生的檔案夾名字,運行,就OK了

<%
' sitemap.asp
session("count")=0
'你的網域名稱
session("server")="http://你的網站地址"

'製作產生SiteMap.xml的目錄,後面不要帶"/",大小寫敏感
vDir = "/XXXXX"

'設定你想要GOOGLE收錄的檔案檔案名稱副檔名
Extensions = Array("png","gif","jpg","jpeg","zip","pdf","ps","html","htm","asp","php","wk1","wk2","wk3","wk4","wk5","wki","wks","wku","lwp","mw","xls","ppt","doc","swf","wks","wps","wdb","wri","rtf","ans","txt","shtml","shtm")

'需要過濾的目錄
PathExclusion=Array("\admin","\_vti_cnf","_vti_pvt","_vti_log","cgi-bin","aspnet_client")

set objfso = CreateObject("Scripting.FileSystemObject")
root = Server.MapPath(vDir)
'response.End
'建立檔案
objfso.createtextfile(server.mappath("/sitemap.xml"))
response.write "<p><strong style='font-family:arial,sans-serif;font-size=12'>建立SiteMap.xml成功!</strong><I style='font-family:arial,sans-serif;font-size=12'> "&server.mappath("/sitemap.xml")&" </I></p>"

response.write "<p><strong style='font-family:arial,sans-serif;font-size=12'>正在索引檔案,請稍侯...</strong></p>"

Set openfileobj=objfso.opentextfile(server.mappath("/sitemap.xml"),8)
openfileobj.writeline"<?xml version='1.0' encoding='UTF-8'?>"
openfileobj.writeline"<urlset xmlns='http://www.google.com/schemas/sitemap/0.84'>"

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

openfileobj.writeline "</urlset>"
response.write "<p><strong style='font-family:arial,sans-serif;font-size=12'>SiteMap.xml產生完畢,共<span style=color:#FF0000> "&session("count")&" </span>個檔案被索引</strong></p>"
set fso = nothing

Sub ShowSubFolders(objFolder)
    Set colFolders = objFolder.SubFolders
    For Each objSubFolder In colFolders
        if folderpermission(objSubFolder.Path) then
              openfileobj.writeline getfilelink(objSubFolder.Path,objSubFolder.dateLastModified)
              Set colFiles = objSubFolder.Files
              For Each objFile In colFiles
                  openfileobj.writeline getfilelink(objFile.Path,objFile.dateLastModified)
              Next
              ShowSubFolders(objSubFolder)
        end if
    Next
End Sub

Function getfilelink(file,datafile)
    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>"&Chr("10")&"<loc>"&server.htmlencode(session("server")&vDir&file)&"</loc>"&Chr("10")&"<lastmod>"&filedate&"</lastmod>"&Chr("10")&"<changefreq>daily</changefreq>"&Chr("10")&"<priority>1.0</priority>"&Chr("10")&"</url>"
        session("count")=session("count")+"1"
        Response.Flush
End Function

Function Folderpermission(pathName)
    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
   
    if len(trim(sFileName)) = 0 then
        FileExtensionIsBad = true
        Exit Function
    end if

    sFileExtension = right(sFileName, len(sFileName) - instrrev(sFileName, "."))
    bFileExtensionIsValid = false
    for each sFileExt in extensions
        if ucase(sFileExt) = ucase(sFileExtension) then
              bFileExtensionIsValid = True
              exit for
        end if
    next
    FileExtensionIsBad = not bFileExtensionIsValid
End Function
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.