Discuz! X3.5是個好用穩定的建站系統,但是唯一美中不足就是沒有生產sitemap地圖的功能,對於各大搜尋引擎的收錄有很大影響。 。
目前雖然DZ外掛有此功能,但基本上都是要收費的,而且費用還不低。
這麼簡單的功能也成了割韭菜的管道,今天分享個借助DZ後台規劃任務輕鬆實現sitemap.xml網站地圖的生成方法。
方法一(只針對論壇版塊產生sitemap.xml):
●建立一個名為「cron_sitemap.php」的檔案;
●複製以下程式碼區的內容到「cron_sitemap.php」中,並儲存檔案;
PS:注意編碼,選擇自己DZ對應的編碼,我自己用的是UTF-8版本DZ
- <?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"?>';
- $sitemap.='<urlset ';
- $sitemap.='xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ';
- $sitemap.='xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
- $sitemap.='xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 ';
- $sitemap.='http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
- $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.'/rss/'.$filename,'w');
- fwrite($fp,$sitemap);
- fclose($fp);
- ?>
複製代碼
●第一次先手動執行一次該排程任務,在站點根目錄會生成一個名為 "/rss/sitemap.xml" 的文件,這樣就完成了網站地圖的生成,一勞永逸,就這麼簡單,不用花一分錢。
Discuz sitemap 網站地圖 完全免費 robots
修改robots.txt檔案,加入
- Sitemap: http://www.tshopping.com.tw/rss/sitemap.xml
複製代碼
以上已親測成功,已可正常使用了: http://www.tshopping.com.tw/rss/sitemap.xml
如要按照板塊及文章列出
建立cron_sitemap_forum_threads.php
程式碼
修改robots.txt檔案,加入
- Sitemap: http://www.tshopping.com.tw/rss/rss.xml
複製代碼 親測成功,已可正常使用了: http://www.tshopping.com.tw/rss/rss.xml
|