修改了 心悦导航的sitemap地图不适配谷歌问题
直接复制替换
/app/index/controller/Sitemap.php
<?php
namespace app\index\controller;
use think\Response;
use think\facade\Request;
use app\model\Source as SourceModel;
class Sitemap
{
public function index()
{
// 检查是否有缓存
$sitemap = cache('sitemap');
if (!$sitemap) {
$SourceModel = new SourceModel();
$map[] = ['status', '=', 1];
$map[] = ['is_delete', '=', 0];
$map[] = ['is_time', '=', 0];
$urls = $SourceModel->where($map)
->field('source_id,title,update_time')
->order('update_time', 'desc')
->select();
// 创建 XML 内容
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
// 添加首页
$xml .= '<url>';
$xml .= '<loc>' . Request::domain() . '</loc>';
$xml .= '<lastmod>' . date('Y-m-d') . '</lastmod>';
$xml .= '<changefreq>daily</changefreq>';
$xml .= '<priority>1.0</priority>';
$xml .= '</url>';
// 添加其他页面
foreach ($urls as $url) {
$url['title'] = preg_replace('/[&!@#%^*()+=\[\]{};:"|,.<>?~\\\\]/', '', $url['title']);
$xml .= '<url>';
$xml .= '<loc>' . Request::domain() . '/s/' . $url['title'] . '.html</loc>';
$xml .= '<lastmod>' . date('Y-m-d', strtotime($url['update_time'])) . '</lastmod>';
$xml .= '<priority>0.8</priority>';
$xml .= '</url>';
$xml .= '<url>';
$xml .= '<loc>' . Request::domain() . '/d/' . $url['source_id'] . '.html</loc>';
$xml .= '<lastmod>' . date('Y-m-d', strtotime($url['update_time'])) . '</lastmod>';
$xml .= '<priority>0.8</priority>';
$xml .= '</url>';
}
$xml .= '</urlset>';
// 缓存 sitemap 24 小时 (86400秒)
cache('sitemap', $xml, 86400);
$sitemap = $xml;
}
// 返回响应
return Response::create($sitemap, 'xml')
->header(['Content-Type' => 'application/xml']);
}
}
License:
CC BY 4.0