Project

General

Profile

修改PHPBB论坛URL生成规则

技术文档
09/29/2012

PHPBB论坛的默认URL地址格式,比如:http://www.saferguard.com/forum/viewforum.php?f=2
http://www.saferguard.com/forum/faq.php#f0r5

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

在phpbbchina.com上找到一篇帖子:真正的phpbb307手写伪静态,忘了那该死的插件吧!

我按着步骤修改,有报错没有修改成功。索性认真研究了下相关的几个文件,嘿嘿,终于OK了。

步骤如下:

第一步:修改根目录下的.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 = '<span class="page-sep">' . $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

/*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 .= '<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 class="page-sep">' . $user->lang['COMMA_SEPARATOR'] . '</span>';
}
$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),

Files