日期:2012-08-28  浏览次数:20577 次

作者:Trotter
邮箱:trotter@kekerde.net
出处:www.gbunix.com

转载请保持文档完整,注明出处。

前言

  随着互联网上的内容以惊人速度的增长也越来越突出了搜索引擎的重要性,如果网站想更好地被搜索引擎收录,网站设计除了面向用户友好(User Friendly)外,搜索引擎友好(Search Engine Friendly)的设计也是非常重要的。进入搜索引擎的页面内容越多,则被用户用不同的关键词找到的几率越大。不得不承认,将动态网页链接rewriting成静态链接是最保险和稳定的面向搜索引擎优化方式。该方案就是针对phpBB论坛系统的URL重定向提出的。

解决方案

  URL重定向从技术上将,目前可以通过两种方式实现,一种是基于URL rewrite,另一种是基于PATH_INFO。例如http://www.gbunix.com/bbs/ftopic102.html就是基于rewrite实现的,而http://www.gbunix.com/article/article.php/515是基于PATH_INFO实现的。

  针对PHPBB论坛的改造,我们分别就这两种技术分别介绍。

一.使用rewrite技术实现:

修改phpBB代码:

打开/includes/page_header.php文件,

搜索代码:

//
// Generate logged in/logged out status
//

之前加:

ob_start();
function replace_for_mod_rewrite(&$s)
{
$urlin =
array(
"'(?<!/)viewforum.php\?f=([0-9]*)&topicdays=([0-9]*)&start=([0-9]*)'",
"'(?<!/)viewforum.php\?f=([0-9]*)&mark=topics'",
"'(?<!/)viewforum.php\?f=([0-9]*)'",
"'(?<!/)viewtopic.php\?t=([0-9]*)&view=previous'",
"'(?<!/)viewtopic.php\?t=([0-9]*)&view=next'",
"'(?<!/)viewtopic.php\?t=([0-9]*)&postdays=([0-9]*)&postorder=([a-zA-Z]*)&start=([0-9]*)'",
"'(?<!/)viewtopic.php\?t=([0-9]*)&start=([0-9]*)&postdays=([0-9]*)&postorder=([a-zA-Z]*)&highlight=([a-zA-Z0-9]*)'",
"'(?<!/)viewtopic.php\?t=([0-9]*)start=([0-9]*)'",
"'(?<!/)viewtopic.php\?t=([0-9]*)'",
"'(?<!/)viewtopic.php&p=([0-9]*)'",
"'(?<!/)viewtopic.php\?p=([0-9]*)'",
);
$urlout = array(
"viewforum\\1-\\2-\\3.html",
"forum\\1.html",
"forum\\1.html",
"ptopic\\1.html",
"ntopic\\1.html",
"ftopic\\1-\\2-\\3-\\4.html",
"ftopic\\1.html",
"ftopic\\1-\\2.html",
"ftopic\\1.html",
"sutra\\1.html",
"sutra\\1.html",
);
$s = preg_replace($urlin, $urlout, $s);
return $s;
}

打开/includes/page_tail.php文件,

搜索代码:

$db->sql_close();

之后加:

$contents = ob_get_contents();
ob_end_clean();
echo replace_for_mod_rewrite($contents);
global $dbg_starttime;

如果你的phpBB是2.06版本,打开includes/functions.php文件,

搜索代码:

if (!empty($db))
{
$db->sql_close();
}

之后加:

if (stristr($url, 'http://')) {
header('Location: ' . $url);
exit;
}

最后在bbs目录下建立.htaccess 文件,文件内容为:

RewriteEngine On
RewriteRule ^forums.* index.php
RewriteRule ^forum([0-9]*).* viewforum.php?f=$1&mark=topic
RewriteRule ^viewforum([0-9]*)-([0-9]*)-([0-9]*).* viewforum.php?f=$1&topicdays=$2&start=$3
RewriteRule ^forum([0-9]*).* viewforum.php?f=$1
RewriteRule ^ptopic([0-9]*).* viewtopic.php?t=$1&view=previous
RewriteRule ^ntopic([0-9]*).* viewtopic.php?t=$1&view=next
RewriteRule ^ftopic([0-9]*)-([0-9]*)-([a-zA-Z]*)-([0-9]*).* viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4
RewriteRule ^ftopic([0-9]*)-([0-9]*).* viewtopic.php?t=$1&start=$2
RewriteRule ^ftopic([0-9]*).* viewtopic.php?t=$1
RewriteRule ^ftopic([0-9]*).html viewtopic.php?t=$1&start=$2&postdays=$3&postorder=$4&highlight=$5
RewriteRule ^sutra([0-9]*).* viewtopic.php?p=$1

如果你的服务器不支持.htaccess,请打开httpd.conf文件,编辑你的虚拟主机部分,如下:

<VirtualHost 1.2.3.4>
ServerAdmin webmaster@domain.com
DocumentRoot /home1/ftp/trotter/www
ServerName www.gbunix.com
RewriteEngine On
RewriteRule ^/bbs/forums.* /bbs/index.php
RewriteRule ^/bbs/forum([0-9]*).* /bbs/viewforum.php?f=$1&mark=topic
RewriteRule ^/bbs/viewforum([0-9]*)-([0-9]*)-([0-9]*).* /bbs/viewforum.php?f=$1&topicdays=$2&start=$3
RewriteRule ^/bbs/forum([0-9]*).* /bbs/viewforum.php