Sitemap. xml generation method (ASP and PHP)

Source: Internet
Author: User

Sitemap.xmlis a site map agreement, which is based on the robots.txt file protocol and upgraded. Websites that submit sitemap. XML to the search engine are more conducive to crawling indexing of Web Crawlers of the search engine. This will improve the efficiency and accuracy of indexing website content.

There are a total of six tags, changefreq: page content update frequency;

Lastmod: The last modification time of the page;

Loc: permanent link address of the page;

Priority: priority relative to other pages (this label can be left empty );

URL: the parent tag relative to the first four tags;

Urlset: the parent label relative to the first five TAGs.

You can provide multiple sitemap files to the search engine. However, each sitemap file must contain no more than 50,000 URLs and cannot exceed 10 MB if it is not compressed.

Submit the website map sitemap to Google at http://www.google.com/webmastersmanagement;

To Yahoo! Submit site map sitemap: Submit through site http://siteexplorer.search.yahoo.com management;

Submit site map sitemap to MSN: submit directly with URL: http://api.moreover.com/ping? U = http % 3A // your. domainname/sitemap. xml. This is the backdoor URL for submitting a website map directly to MSN. Note ":" is replaced by % 3A.

Submit the website map sitemap to ask. Http://submissions.ask.com/ping? Sitemap = http % 3A // your. domainname/sitemap. xml. Note ":" is replaced by % 3A.

The format of sitemap. XML is as follows:


<? 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>

How to Create sitemap. xml. The most stupid way is to follow the rules of these six labels and write them manually.

If there are too many pages on the website, this will become a super depressing physical labor. So there are many sitemaps. XML generation tool appears, but now most sitemap. the XML generation tool inputs the website address on the client, allowing the tool to search for Link generation on the website. In this mode, rookie feels less efficient and there is no way to control the generated link. Finally, I found a better method on the Internet, which is suitable for websites that generate static pages of content. Someone writes the sitemap. xml generation function into ASP and PHP pages, and controls the links to be generated on the pages. After modifying the page as needed, upload the page to your website space. Accessing this page is the sitemap. xml file you need. Save it as an XML file format, upload it to your space, and submit the link to a search engine that supports sitemap. xml.

ASP file, copy the blue code to the text file, save it as sitemap. asp, modify the settings, upload it to the server, and access it.

<% SESSION ("server") = "http://www.grzz.com.cn" 'change this http://www.grzz.com.cn to your domain name vdir = "/"'. 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. filesfor each objfile in colfilesresponse. write getfilelink (objfile. path, objfile. datelastmodified) nextshowsubfolders (objfolder) response. write "</urlset>" set FSO = nothingsub showsubfolders (objfolder) set colfolders = objfolder. subfoldersfor each objsubfolder in colfoldersif folderpermission (objsubfolder. path) thenresponse. write getfilelink (objsubfolder. path, objsubfolder. datelastmodified) set colfiles = objsubfolder. filesfor each objfile in colfilesresponse. write getfilelink (objfile. path, objfile. updated) updated (ended) end ifnextend subfunction getfilelink (file, datafile) 'changefreq change parameters: Always, hourly, daily, weekly, monthly, yearly, neverfile = Replace (file, root, "") file = Replace (file, "\", "/") if fileextensionisbad (File) Then exit functionif 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> example &server.html encode (Session (" server ") & vdir & file) & "</loc> <lastmod>" & filedate & "</lastmod> <changefreq> weekly </changefreq> </URL>" response. flushend functionfunction folderpermission (pathname) 'Directory to be filtered (not listed in sitemap) pathexclusion = array ("\ Ad", "\ ADMIN", "\ aspnet_client ", "\ count", "\ data", "\ Inc", "\ upload", "\ template") folderpermission = truefor each pathexcluded in pathexclusionif instr (ucase (pathname ), ucase (pathexcluded)> 0 bytes = falseexit forend ifnextend functionfunction fileextensionisbad (sfilename) dim sfileextension, bfileextensionisvalid, sfileextextensions = array ("html") 'sets the name, if the extension is not included, sitemap will not include the file with the extension if Len (TRIM (sfilename) = 0 thenfileextensionisbad = trueexit functionend ifsfileextension = right (sfilename, Len (sfilename) -limit Rev (sfilename, ".) bfileextensionisvalid = false 'assume extension is badfor each sfileext in extensionsif ucase (sfileext) = ucase (sfileextension) protocol = trueexit forend ifnextfileextensionisbad = Not functions %>

PHP file, copy the green code to the text file, save it as sitemap. php, modify the relevant settings, upload it to the server, and access it.

<? Phpheader ('content-type: Application/XML; charset = "gb2312", true);?> <? PHP $ website = "http://www.grzz.com.cn";/* change this http://www.grzz.com.cn to your domain name */$ page_root = "/"; /* change to the directory address of your website * // * changefreq can be set by yourself */$ changefreq = "Weekly"; // "always", "hourly", "Daily ", "Weekly", "Monthly", "yearly" and "never ". /* modification time */$ last_modification = date ("Y-m-d \ th: I: s "). substr (date ("o ). ":". substr (date ("O"), 3);/* directory to be generated */$ allow_dir [] = "Web "; /* directory to be filtered (not listed in sitemap) */$ disallo W_dir [] = "admin"; $ disallow_dir [] = "_ notes";/* set the name of the list file, if the extension is not included, sitemap will not include the file with the extension */$ disallow_file [] = ". INC "; $ disallow_file [] =". old "; $ disallow_file [] =". save "; $ disallow_file [] = cmd.txt"; $ disallow_file [] = ". JS "; $ disallow_file [] =" ~ "; $ Disallow_file [] = ". LCK "; $ disallow_file [] = pai.zip"; $ disallow_file [] = ". zip "; $ disallow_file [] =". CSV "; $ disallow_file [] = pai.csv"; $ disallow_file [] = pai.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 ($ ARRA Y 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 (); forea CH ($ array as $ Val) {$ res [] = str_replace ($ old_offset, $ offset, $ Val);} return $ res ;} /* This walks recursivly through all directories starting at page_root andadds 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 fileswhile ($ file = readdir ($ DIR) {// make sure the file existsif ($ file! = "." & $ File! = ".." & $ File [0]! = '. ') {// If it's a ctiry ry, 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 {// filesif (fl_contains ($ file, $ disallow_file) continue; array_push ($ TMP, str_replace ($ directory_orig, $ Directory_offset, $ directory. "/". $ file) ;}}// finish off the functionclosedir ($ 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 ). ":". substr (date ("o"), 3);?> </Lastmod> <changefreq> <? Echo utf8_encode ($ changefreq);?> </Changefreq> </URL> <?}?> </Urlset>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.