thinkphp generating multi-sitemap files
We know that sitemap for the importance of SEO, a lot of introduction only to generate a file sitemap.xml, but if the site content is more, it is necessary to generate multiple sitemap files, because the search engine on the sitemap file size and number of restrictions, Google, for example, limits each sitemap file to 50,000 data.
What is the multi-sitemap file mechanism? First we generate a primary sitemap file, which is the Sitemapindex type, which holds the path to the child sitemap file. The child sitemap file is used to store the specific article item. Here we assume that each sub-Sitemap has a number of 10,000 URLs. The code is as follows (the thinkphp framework used here is the same principle):
classSitemapactionextendsAction {//Generate sitemap Public functionCreate () {$page _size= 10000;//number of bars per page $BP _db= M (' baobeiproducts '); //1w Address to generate a sub-map, determine how many to generate? $count=$BP _db->where (' status = 1 ')Count(); $page _count=Ceil($count/$page _size);//divide several files $this->create_index ($page _count);//generate the main sitemap $this->create_child ($page _count,$page _size);//Create a child sitemap $this->success (' Map generation succeeded '); } //generate the main sitemap protected functionCreate_index ($page _count) { $content= "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\r\n<sitemapindex xmlns=\" http://www.sitemaps.org/schemas/ Sitemap/0.9\ ">\r\n"; for($i= 1;$i<=$page _count;$i++) { $content. = "<sitemap>\r\n<loc> Http://HOST/sitemap/sitemap".$i.". Xml</loc>\r\n<lastmod> ".Date(' y-m-d '). " </lastmod>\r\n</sitemap> "; } $content. = "</sitemapindex>"; $file=fopen("Sitemap.xml", "W"); fwrite($file,$content); fclose($file); } //Create a child sitemap protected functionCreate_child ($page _count,$page _size) { for($i= 0;$i<$page _count;$i++) { $list= M (' baobeiproducts ')->field (' Id,m_time ')->order (' ID ASC ')->limit ($i*$page _size.‘,‘.$page _size),Select (); $sitemap= "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\r\n<urlset xmlns=\" http://www.sitemaps.org/schemas/sitemap/ 0.9\ ">\r\n"; foreach($list as $k=$v){ $sitemap. = "<url>\r\n". " <loc>http://host/baobei/".$v[' ID ']. " </loc>\r\n "." <priority>0.6</priority>\r\n<lastmod> ".Date(' y-m-d ',$v[' M_time ']). " </lastmod>\r\n<changefreq>weekly</changefreq>\r\n</url>\r\n "; } $sitemap. = ' </urlset> '; $file=fopen("Sitemap/sitemap". ($i+1). ". XML "," W "); fwrite($file,$sitemap); fclose($file); } } }
Generate Multi-sitemap files