<?php
header('Content-Type: application/xml');
// 禁止用户直接访问口文件
defined("ECMSAPI_MOD") or exit;
// 设置项
$set_cache_time = 60 * 60 * 12; // 设置缓存时间
// sys_ReturnBqTitleLink 是帝国的内置函数,用来获取内容链接。
// 参数为数组,必须包含 id,classid,newspath,filename,groupid,titleurl字段
$type = $_GET['type'];
$tbname = $_GET['tbname'];
$num = $type == 'shenma' ? 10000 : 50000;
$WebSite = (isHTTPS() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];
$cache = $api->load('cache'); // 获取缓存类对象
$cacheName = $type . '_' . $tbname . '_' . $num; //定义一个独一无二的缓存变量名称
$list = $cache->get($cacheName); // 获取缓存
$LastChangeTime = $cache->get($cacheName . 'LastChangeTime'); //获取最后修改日期
if(NULL === $list){
// 若缓存不存在,或缓存过期。重新获取数据
$list = $api->load('db')->select("[!db.pre!]ecms_{$tbname}" , 'id,classid,newspath,filename,groupid,titleurl' , '1' , "{$num},{$_GET['no']}" , 'id desc');
$cache->set($cacheName , $list , $set_cache_time);
$cache->set($cacheName . 'LastChangeTime', time() , $set_cache_time);
$LastChangeTime = time();
}
// 输出内容
$output = '<?xml version="1.0" encoding="utf-8"?>' . "\r\n";
if ($type=='baidu'){
$output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">' . "\r\n";
} else if ($type=='google'){
$output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\r\n";
} else {
$output .= '<urlset>' . "\r\n";
}
foreach($list as $v){
$output .= ' <url>' . "\r\n";
$output .= ' <loc>' . $WebSite . sys_ReturnBqTitleLink($v) . '</loc>' . "\r\n";
if ($type=='baidu'){
$output .= ' <mobile:mobile type="htmladapt"/>';
}
$output .= ' <changefreq>daily</changefreq>' . "\r\n";
$output .= ' <priority>0.9</priority>' . "\r\n";
$output .= ' </url>' . "\r\n";
}
$output .= '</urlset>';
$etag = md5($output);
header ("ETag: \"{$etag}\""); //设置etag-header
header ("Last-Modified: " . gmdate ('D, d M Y H:i:s \G\M\T', $LastChangeTime));
header ("Expires: " . gmdate ('D, d M Y H:i:s \G\M\T', ($LastChangeTime + $set_cache_time)));
header ("Cache-Control: max-age=" . $set_cache_time );
// 如果存在缓存头,返回304
if (time() - $LastChangeTime < $set_cache_time) {
$ifmod = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] == gmdate ('D, d M Y H:i:s \G\M\T', $LastChangeTime) : null;
$iftag = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] == 'W/"'.$etag.'"' : null;
if (($ifmod || $iftag) && ($ifmod !== false && $iftag !== false)) {
$api->sendCode(304);
//var_dump($ifmod,$iftag,$_SERVER['HTTP_IF_NONE_MATCH'],$_SERVER['HTTP_IF_NONE_MATCH'],$etag);
exit;
}
}
echo $output;
?>
Nginx伪静态规则:
rewrite "^/sitemap_([0-9a-z]+?)_([a-z]+?)_([0-9]+?)\.xml" /ecmsapi/index.php?mod=map&act=sitemap&type=$1&tbname=$2&no=$3 last;
地图链接格式:
https://www.xxx.com/sitemap_shenma_news_1.xml
https://www.xxx.com/sitemap_baidu_news_1.xml
https://www.xxx.com/sitemap_360_news_1.xml
https://www.xxx.com/sitemap_google_news_1.xml
https://www.xxx.com/sitemap_bing_news_1.xml
功能特色:
- 带MD5【etag】标签头生成
- 支持缓存和浏览器缓存,默认12小时