zoukankan      html  css  js  c++  java
  • 生成多sitemap文件

    Thinkphp生成多sitemap文件  

    我们知道sitemap对于seo的重要性,很多介绍只生成一个文件sitemap.xml ,但是如果网站内容比较多,就要生成多个sitemap文件,因为搜索引擎对sitemap文件大小和条数有限制,比如google对每个sitemap文件的限制为5万条数据。

    何为多sitemap文件机制? 首先我们生成一个主sitemap文件,此文件为sitemapindex类型,其中存放子sitemap文件的路径。子sitemap文件用来存放具体文章item. 这里我们假定每个子sitemap存放网址数为10000个。则代码如下(这里用的thinkphp框架,原理都是一样的):

    class SitemapAction extends Action { 
         
       //生成sitemap 
        public function create() {
                    $page_size    =    10000; //每页条数
                    $bp_db    =    M('BaobeiProducts');
                    //1w个地址生成一个子地图,判断需要生成几个?
                    $count        =    $bp_db->where('status = 1')->count();
                    $page_count    =    ceil($count/$page_size);  //分几个文件
                    
                    $this->create_index($page_count);    //生成主sitemap
                    $this->create_child($page_count,$page_size);    //生成子sitemap
                    
     
                $this->success('地图生成成功'); 
            
        }
        
        //生成主sitemap
        protected function create_index($page_count) {
        
                    $content    =    "<?xml version="1.0" encoding="utf-8"?>
    <sitemapindex xmlns=" http://www.sitemaps.org/schemas/sitemap/0.9">
    ";
                    for($i=1;$i<=$page_count;$i++) {
                        
                            $content    .="<sitemap>
    <loc> http://HOST/sitemap/sitemap".$i.".xml</loc>
    <lastmod>".date('Y-m-d')."</lastmod>
    </sitemap>";
                    }
                    $content .= "</sitemapindex>";
                    
                    $file = fopen("sitemap.xml","w"); 
                fwrite($file,$content); 
                fclose($file);
        
        
       }
       
       //生成子sitemap
       protected function create_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"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    "; 
                        foreach($list as $k=>$v){ 
                            $sitemap .= "<url>
    "."<loc>http://HOST/baobei/".$v['id']."</loc>
    "."<priority>0.6</priority>
    <lastmod>".date('Y-m-d',$v['m_time'])."</lastmod>
    <changefreq>weekly</changefreq>
    </url>
    "; 
                 
                        } 
                     
                    $sitemap .= '</urlset>'; 
                     
                    $file = fopen("sitemap/sitemap".($i+1).".xml","w"); 
                    fwrite($file,$sitemap); 
                    fclose($file);
               }
           } 
     
    }
  • 相关阅读:
    Codeforces 1132D
    Codeforces 670F
    Codeforces 670E
    Codeforces 670E
    Codeforces 670E
    Codeforces 670
    Codeforces 1138
    Codeforces 1114E
    力扣21.合并两个有序链表
    力扣538.把二叉树转换为累加树
  • 原文地址:https://www.cnblogs.com/goldenstones/p/6232403.html
Copyright © 2011-2022 走看看