你现在的位置:首页 > PHP网站建设知识库 > phpbb > 正文

phpbb开发之:修改phpbb论坛URL规则

我们做phpbb开发,都知道PHPBB论坛的默认URL地址格式,

比如:

http://www.yourdomain.com/forum/viewforum.php?f=2
http://www.yourdomain.com/forum/faq.php#f0r5


这样的URL格式不利于SEO的优化。我们想更改默认格式,最终显示结果:
把/forum/viewforum.php?f=2  改成   /forum/viewforum-2.html                                 
把/forum/faq.php#f0r5   改成  /forum/faq-f0r5.html

步骤如下:
第一步:修改根目录下的.htaccess文件,定义URL的规则
在文件的结果加上:
RewriteEngine on
#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. 查找:'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
     改为:'U_VIEW_FORUM' => "./viewforum". '-' . $forum_id.'.html',
 2. 查找:$pagination = generate_pagination(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id" . ((strlen($u_sort_param)) ? "&$u_sort_param" : '') . (($highlight_match) ? "&hilit=$highlight" : '')), $total_posts, $config['posts_per_page'], $start);
    改为:$pagination = generate_pagination1(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id" . ((strlen($u_sort_param)) ? "&$u_sort_param" : '') . (($highlight_match) ? "&hilit=$highlight" : '')),"./viewtopic". '-' . $forum_id.'-'. $topic_id.'-', $total_posts, $config['posts_per_page'], $start);

第三步:修改includes/functions.php
在最后加上函数generate_pagination1,代码如下:
/*Arlinger info*/
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 = '' . $user->lang['COMMA_SEPARATOR'] . '';
$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) ? '1' : '1';
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) ? '' . $i . '' : '' . $i . '';
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) ? '' . $i . '' : '' . $i . '';
if ($i < $total_pages)
{
$page_string .= $seperator;
}
}
}
$page_string .= ($on_page == $total_pages) ? '' . $total_pages . '' : '' . $total_pages . '';
if ($add_prevnext_text)
{
if ($on_page != 1)
{
$page_string = '' . $user->lang['PREVIOUS'] . '  ' . $page_string;
}
if ($on_page != $total_pages)
{
$page_string .= '  ' . $user->lang['NEXT'] . '';
}
}
$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
/*Arlinger info*/
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 .= '' . $times . '';
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 .= '' . $user->lang['COMMA_SEPARATOR'] . '';
}
$times++;
}
}
else
{
$pagination = '';
}
return $pagination;
}

2. 查找:'U_VIEWFORUM'  => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']))
   改为:'U_VIEWFORUM'   => "./viewforum". '-' . $row['forum_id'].".html")
3. 查找:'link'  => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $subforum_id),
   改为:'link'  => "./viewforum". '-' . $subforum_id.'.html',
4. 查找:$u_viewforum = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']);
   改为:$u_viewforum = "./viewforum". '-' . $row['forum_id'].'.html';
5. 查找:if (($row['forum_flags'] & FORUM_FLAG_LINK_TRACK) || $row['forum_password'] || !$auth->acl_get('f_read', $forum_id))
   {
    $u_viewforum = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $row['forum_id']);
   改为:if (($row['forum_flags'] & FORUM_FLAG_LINK_TRACK) || $row['forum_password'] || !$auth->acl_get('f_read', $forum_id))
   {
    $u_viewforum = "./viewforum". '-' . $row['forum_id'].'-'.$start.'.html';
6. 查找:'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $parent_forum_id))
   改为:'U_VIEW_FORUM' => "./viewforum". '-' . $parent_forum_id.'.html')
7. 查找:'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_data['forum_id']))
   改为:'U_VIEW_FORUM' => "./viewforum". '-' . $forum_data['forum_id'].'.html')
8. 查找:'U_ACTIVE_FORUM'  => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $active_f_id),
   改为:'U_ACTIVE_TOPIC'  => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=' . $active_t_id),

第五步:修改viewforum.php
1. 查找:$view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", $view_topic_url_params);
   改为:$view_topic_url = "./viewtopic" . '-' . $topic_id.'.html';
2. 查找:'U_VIEW_FORUM'  => append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id&start=$start"),
   改为:'U_VIEW_FORUM' => "./viewforum". '-' . $forum_id.'-'.$start.'.html',
3. 查找:'S_FORUM_ACTION'  => append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id&start=$start"),
   改为:'S_FORUM_ACTION'        => "./viewforum". '-' . $forum_id.'-'.$start.'.html',
4. 查找:'PAGINATION' => generate_pagination(append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . ((strlen($u_sort_param)) ? "&$u_sort_param" : '')), $topics_count, $config['topics_per_page'], $start),
   改为:'PAGINATION' => generate_pagination1(append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id" . ((strlen($u_sort_param)) ? "&$u_sort_param" : '')),"./viewforum". '-' . $forum_id.'-', $topics_count, $config['topics_per_page'], $start),
5. 查找:'PAGINATION'  => topic_generate_pagination($replies, $view_topic_url),
   改为:'PAGINATION'     => topic_generate_pagination1($replies, $view_topic_url1)