将以下PHP文件放到网站的根目录。
加入此sitemap的前提:
必须将PHPBB论坛的默认URL规格改成以下文件里生成的格式。
具体方法,请参考:
http://www./article/13591.html
注意:
红色部分的代码,修改成自己网站对应的内容:
<?php
// By: Arlinger On 2012-6-4
//
// CUSTOM INSTALLATION VARIABLES:
$subdomain='www.';
$domain=' '; //这里要改成你的网站名
$folder='/forum/'; //这里要改成你把博客所在的文件夹的名字
$urls=50000;
// Change Nothing Below Here:
$domainpath='http://'.$subdomain.$domain.$folder;
// Connect
define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'config.' . $phpEx);
include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
$db = new $sql_db();
$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false);
// Gzip compression
if (@extension_loaded('zlib') && !headers_sent())
{
ob_start('ob_gzhandler');
}
// Send Header
header("Content-Type: text/xml;charset=iso-8859-1");
// Send initial Data
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
// Send Index
$current_time=time();
echo '<url>
<loc>'.$domainpath.'</loc>
<lastmod>'.date("Y-m-d\TH:i:sP",$current_time).'</lastmod>
</url>';
$urls=$urls-1;
// Send Forums
$sql = 'SELECT forum_id, forum_last_post_time FROM '.$table_prefix.'forums WHERE enable_indexing=1 LIMIT '.$urls;
$result = $db->sql_query($sql);
while ($data=$db->sql_fetchrow($result)) {
// Fix for parents forums showing 1969
if ($data['forum_last_post_time'] == 0) {
$data['forum_last_post_time']=$current_time;}
echo '<url>
<loc>'.$domainpath.'viewforum-'.$data['forum_id'].'.html'.'</loc>
<lastmod>'.date("Y-m-d\TH:i:sP",$data['forum_last_post_time']).'</lastmod>
</url>';
$urls=$urls-1;
}
$db->sql_freeresult($result);
// Send Topics
$sql = 'SELECT * FROM '.$table_prefix.'topics as t, '.$table_prefix.'forums as f WHERE t.topic_approved=1 AND t.forum_id=f.forum_id AND f.enable_indexing=1 ORDER BY t.topic_id DESC LIMIT '.$urls;
$result = $db->sql_query($sql);
while ($data=$db->sql_fetchrow($result)) {
echo '<url>
<loc>'.$domainpath.'viewtopic-'.$data['topic_id'].'.html'.'</loc>
<lastmod>'.date("Y-m-d\TH:i:sP",$data['topic_last_post_time']).'</lastmod>
</url>';
}
$db->sql_freeresult($result);
// Send Closing Tags
echo ' </urlset>';
?>
本节内容:
PHPBB论坛URL生成规则
PHPBB论坛的默认URL地址格式,比如:
http://www./forum/faq.php#f0r5
这样的URL格式不利于SEO的优化。
更改默认格式,最终显示结果:
把/forum/faq.php#f0r5 改成 /forum/faq-f0r5.html
步骤如下:
第一步:修改根目录下的.htaccess文件,定义URL的规则
在文件的结尾加上:
#phpbb重写规则
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^viewtopic-(.+)-(.+).html$ viewtopic.php?f=$1&t=$2&start=$3 [L,NC]
RewriteRule ^viewtopic-(.+)-(.+).html$ viewtopic.php?f=$1&t=$2 [L,NC]
RewriteRule ^viewtopic-(.+).html$ viewtopic.php?t=$1 [L,NC]
RewriteRule ^viewforum-(.+)-(.+).html$ viewforum.php?f=$1&start=$2 [L,NC]
RewriteRule ^viewforum-(.+).html$ viewforum.php?f=$1 [L,NC]
第二步:修改viewtopic.php
1、查找:
改为:
2、查找:
改为:
第三步:修改includes/functions.php
在最后加上函数generate_pagination1,代码如下:
function generate_pagination1($base_url1,$base_url, $num_items, $per_page, $start_item, $add_prevnext_text = false, $tpl_prefix = '')
{
global $template, $user;
// Make sure $per_page is a valid value
$per_page = ($per_page <= 0) ? 1 : $per_page;
$seperator = '<span >' . $user->lang['COMMA_SEPARATOR'] . '</span>';
$total_pages = ceil($num_items / $per_page);
if ($total_pages == 1 || !$num_items)
{
return false;
}
$on_page = floor($start_item / $per_page) + 1;
$url_delim = (strpos($base_url, '?') === false) ? '?' : ((strpos($base_url, '?') === strlen($base_url) - 1) ? '' : '&');
$page_string = ($on_page == 1) ? '<strong>1</strong>' : '<a href="' . $base_url . '0.html">1</a>';
if ($total_pages > 5)
{
$start_cnt = min(max(1, $on_page - 4), $total_pages - 5);
$end_cnt = max(min($total_pages, $on_page + 4), 6);
$page_string .= ($start_cnt > 1) ? ' ... ' : $seperator;
for ($i = $start_cnt + 1; $i < $end_cnt; $i++)
{
$page_string .= ($i == $on_page) ? '<strong>' . $i . '</strong>' : '<a href="' . $base_url . "" . (($i - 1) * $per_page) . '.html">' . $i . '</a>';
if ($i < $end_cnt - 1)
{
$page_string .= $seperator;
}
}
$page_string .= ($end_cnt < $total_pages) ? ' ... ' : $seperator;
}
else
{
$page_string .= $seperator;
for ($i = 2; $i < $total_pages; $i++)
{
$page_string .= ($i == $on_page) ? '<strong>' . $i . '</strong>' : '<a href="' . $base_url . "" . (($i - 1) * $per_page) . '.html">' . $i . '</a>';
if ($i < $total_pages)
{
$page_string .= $seperator;
}
}
}
$page_string .= ($on_page == $total_pages) ? '<strong>' . $total_pages . '</strong>' : '<a href="' . $base_url . "" . (($total_pages - 1) * $per_page) . '.html">' . $total_pages . '</a>';
if ($add_prevnext_text)
{
if ($on_page != 1)
{
$page_string = '<a href="' . $base_url . "" . (($on_page - 2) * $per_page) . '.html">' . $user->lang['PREVIOUS'] . '</a> ' . $page_string;
}
if ($on_page != $total_pages)
{
$page_string .= ' <a href="' . $base_url . "" . ($on_page * $per_page) . '.html">' . $user->lang['NEXT'] . '</a>';
}
}
$template->assign_vars(array(
$tpl_prefix . 'BASE_URL' => $base_url,
'A_' . $tpl_prefix . 'BASE_URL' => addslashes()($base_url1),
$tpl_prefix . 'PER_PAGE' => $per_page,
$tpl_prefix . 'PREVIOUS_PAGE' => ($on_page == 1) ? '' : $base_url . "" . (($on_page - 2) * $per_page.".html"),
$tpl_prefix . 'NEXT_PAGE' => ($on_page == $total_pages) ? '' : $base_url . "" . ($on_page * $per_page).".html",
$tpl_prefix . 'TOTAL_PAGES' => $total_pages,
));
return $page_string;
}
第四步:修改includes/functions_display.php
1. 在最后加上函数topic_generate_pagination1
function topic_generate_pagination1($replies, $url)
{
global $config, $user;
// Make sure $per_page is a valid value
$per_page = ($config['posts_per_page'] <= 0) ? 1 : $config['posts_per_page'];
if (($replies + 1) > $per_page)
{
$total_pages = ceil(($replies + 1) / $per_page);
$pagination = '';
$times = 1;
for ($j = 0; $j < $replies + 1; $j += $per_page)
{
$pagination .= '<a href="' . $url . '' . $j . '.html">' . $times . '</a>';
if ($times == 1 && $total_pages > 5)
{
$pagination .= ' ... ';
// Display the last three pages
$times = $total_pages - 3;
$j += ($total_pages - 4) * $per_page;
}
else if ($times < $total_pages)
{
$pagination .= '<span >' . $user->lang['COMMA_SEPARATOR'] . '</span>';
}
$times++;
}
}
else
{
$pagination = '';
}
return $pagination;
}
2. 查找:
改为:
3. 查找:
改为:
4. 查找:
改为:
5. 查找:
{
$u_viewforum = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']);
改为:
{
$u_viewforum = "./viewforum". '-' . $row['forum_id'].'-'.$start.'.html';
6. 查找:
改为:
7. 查找:
改为:
8. 查找:
改为:
第五步:修改viewforum.php
1. 查找:
改为:
2. 查找:
改为:
3. 查找:
改为:
4. 查找:
改为:
5. 查找:
改为:
本节内容:
php sitemap文件生成器
例子:
<?php
#---------------------------------------
# PHP版的Google Sitemap 生成器 ver 0.1
# 注意:必须对当前目录有写的权限
#---------------------------------------
#网站根域名
$WebRoot = " 改成自己的网址
#XML文件名称
$XMLFile = "sitemaps.xml";
#要建虑的目录[区分大小写],注意:前面加号是因为0在PHP中表示假,这样取子串位置时就不会返回假
#以本程序所在的目录为当前目录,即扫描的根目录,所以目录前面不用加上"/"
$FilterDir = "+|sysAdmin|down|";
#要索引的文件扩展名[小写]
$IndexFileExt = "+|htm|html|";
#XML头部
$XMLText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"";
#XML尾部
$XMLEndText = "</urlset>";
echo "开始构建文件XML索引...";
DealFP(".");
$XMLText .= $XMLEndText;
makeFile($XMLFile,$XMLText);
echo "ok!<br><br>";
$url = $WebRoot.$XMLFile;
echo "<a href="/blog_article/.$url">打开</a>:".$url;
#公用函数库:
#新建文件
function makeFile($fileName, $text){
$fp = fopen($fileName, "w+");
fwrite($fp, $text);
fclose($fp);
}
/**
* 将指定内容添加到XML中
* $f 含相对路径的文件名称
* $dt 日期时间型
*/
function addToXML($f, $dt){
$s = "<url><loc>".$GLOBALS["WebRoot"].$f."</loc><lastmod>".$dt."</lastmod></url>\n";
$GLOBALS["XMLText"] .= $s;
}
/**
* 遍历指定的目录以及子目录,将符合条件的文件加入XML
* $p 指定的目录
*/
function DealFP($p){
$FilterDir = $GLOBALS["FilterDir"];
$IndexFileExt = $GLOBALS["IndexFileExt"];
$handle=opendir($p);
if ($p==".") $path = "";
else $path = $p."/";
while ($file = readdir($handle))
{ // www.
$d = filetype($path.$file);
if ((($d=='file')||($d=='dir'))&&($file!='.')&&($file!='..'))
{
$pf = $path.$file;
//echo "[".$d."]".$pf."<br>";
if ($d=='dir')
{
if (!(strpos($FilterDir, "|".$pf."|")))
{
DealFP($pf);
}
}else{
$ext = "|".strtolower()(substr($file, strrpos($file, ".")+1))."|";
if (strpos($IndexFileExt, $ext))
{
$d = filemtime($pf);
$dt = date("Y-m-d",$d)."T".date("H:i:s",$d)."+00:00";
addToXML($pf, $dt);
}
}
}
}
closedir($handle);
}
?>