使用定时任务在Discuz X3.4中免插件自动生成sitemap.xml

为了更好的利于SEO,加快百度、360、谷歌、bing、搜狗等收录,我们需要网站生成sitemap.xml,有些搜索引擎只支持.xml,不支持.txt,所以我们必须在511遇见论坛下自动生成sitemap.xml,由于我们采用了伪静态,所以生成的链接必须和伪静态规则一致,在discuz后台插件搜索了几个,姑且不谈收费免费,发现很不稳定,且静态规则无法兼容,这里我们采用discuz的定时任务,自动生成sitemap.xml。

手动建立一个cron_sitemap.php文件复制以下内容:

<?php
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}

$filename='sitemap.xml';
//以下五项根据具体情况修改即可
$cfg_updateperi='60';//协议文件更新周期的上限,单位为分钟
$web_root=$_G['siteurl'];//根网址
$CHARSET='utf-8';// or gbk //选择编码方式
/***********************************************************************************************/
//网站地图sitemap.xml
$sitemap="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$sitemap.="<urlset\n";
$sitemap.="xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n";
$sitemap.="xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
$sitemap.="xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9\n";
$sitemap.="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">\n";
$querys = DB::query("SELECT a.tid FROM ".DB::table('forum_thread')." a inner join ".DB::table('forum_forum')." b on a.fid=b.fid ORDER BY a.tid DESC LIMIT 0,10000");
while($threadfid = DB::fetch($querys))
{
$turl=$web_root.'thread-'.$threadfid['tid'].'-1-1.html';//注意静态规则
$link = $turl;
$t=time();
$riqi=date("Y-m-d",$t);
$priority=rand(1,10)/10;
//date("D F d Y",$t);
$sitemap.="<url>\n";
$sitemap.="<loc>$link</loc>\n";
$sitemap.="<priority>$priority</priority>\n";
$sitemap.="<lastmod>$riqi</lastmod>\n";
$sitemap.="<changefreq>weekly</changefreq>\n";
$sitemap.="</url>\n";
}
$sitemap .= "</urlset>\n";
$fp = fopen(DISCUZ_ROOT.'/'.$filename,'w');
fwrite($fp,$sitemap);
fclose($fp);
?>

注意:自己discuz的编码,这里我选择的是utf-8,注意手动修改。

把cron_sitemap.php文件上传到论坛目录
把cron_sitemap.php上传至source\include\cron目录
discuz后台添加定时任务
后台–工具–计划任务–新增,名字随便,提交
然后编辑,任务脚本:cron_sitemap.php

 

对于任务的设置,大家可以参考我的:

 

补充:之前的使用的生成文件google在读取的时候出了点问题,后经过修改沿用一下版本:

<?php
if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

$filename = 'sitemap.xml';
// 以下五项根据具体情况修改即可
$cfg_updateperi = '60'; // 协议文件更新周期的上限,单位为分钟
$web_root = 'https://www.abc.xyz/'; // 使用目标域名直接设置
$CHARSET = 'utf-8'; // or gbk //选择编码方式
/***********************************************************************************************/
// 网站地图sitemap.xml
$sitemap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$sitemap .= "<urlset\n";
$sitemap .= "xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n";
$sitemap .= "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
$sitemap .= "xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9\n";
$sitemap .= "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">\n";

$querys = DB::query("SELECT a.tid FROM " . DB::table('forum_thread') . " a INNER JOIN " . DB::table('forum_forum') . " b ON a.fid=b.fid ORDER BY a.tid DESC LIMIT 0,10000");
while ($threadfid = DB::fetch($querys)) {
    $turl = $web_root . 'thread-' . $threadfid['tid'] . '-1-1.html'; // 注意静态规则
    $link = htmlspecialchars($turl, ENT_XML1 | ENT_QUOTES, 'UTF-8'); // 使用htmlspecialchars转义特殊字符
    $t = time();
    $riqi = date("c", $t); // 使用ISO 8601日期格式
    $priority = rand(1, 10) / 10;

    $sitemap .= "<url>\n";
    $sitemap .= "<loc>$link</loc>\n";
    $sitemap .= "<priority>$priority</priority>\n";
    $sitemap .= "<lastmod>$riqi</lastmod>\n";
    $sitemap .= "<changefreq>weekly</changefreq>\n";
    $sitemap .= "</url>\n";
}

$sitemap .= "</urlset>\n";

$filepath = DISCUZ_ROOT . '/' . $filename;
$fp = fopen($filepath, 'w');
if ($fp) {
    fwrite($fp, $sitemap);
    fclose($fp);
    echo "Sitemap generated successfully at: $filepath";
} else {
    echo "Error: Unable to open file for writing.";
}
?>

 

 

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注