<?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小时
- 承接帝国模板、插件开发,有需求请联系qq:2388060488
新用户 9月前
2楼支持大哥分享,要是都像大哥这样这个插件早普及使用了
jomol 8月前
3楼感谢,已用上。
hfs446555469 5月前
4楼报错
This page contains the following errors:
error on line 3 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.
xiaobai 3月前
5楼This XML file does not appear to have any style information associated with it. The document tree is shown below.
提示这个是怎么回事啊
shuangrong 2月前
6楼判断网址协议可能这样更好点
$WebSite = (((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];$WebSite = (isHTTPS() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];这种格式在360或谷歌执行不了
supz 1月前
7楼shuangrong 判断网址协议可能这样更好点 $WebSite = (((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') | ...
function isSecure() {
return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443;
}