<span class="postbody">作者:Trotter <br>邮箱:trotter@kekerde.net <br>出处:www.gbunix.com <br><br>转载请保持文档完整,注明出处。 <br><br>前言 <br><br> 随着互联网上的内容以惊人速度的增长也越来越突出了搜索引擎的重要性,如果网站想更好地被搜索引擎收录,网站设计除了面向用户友好(User Friendly)外,搜索引擎友好(Search Engine Friendly)的设计也是非常重要的。进入搜索引擎的页面内容越多,则被用户用不同的关键词找到的几率越大。不得不承认,将动态网页链接rewriting成静态链接是最保险和稳定的面向搜索引擎优化方式。该方案就是针对phpBB论坛系统的URL重定向提出的。 <br><br>解决方案 <br><br> URL重定向从技术上将,目前可以通过两种方式实现,一种是基于URL rewrite,另一种是基于PATH_INFO。例如http://www.gbunix.com/bbs/ftopic102.html就是基于rewrite实现的,而http://www.gbunix.com/article/article.php/515是基于PATH_INFO实现的。 <br><br> 针对PHPBB论坛的改造,我们分别就这两种技术分别介绍。 <br><br>一.使用rewrite技术实现: <br><br>修改phpBB代码: <br><br>打开/includes/page_header.php文件, <br><br>搜索代码: <br><br>// <br>// Generate logged in/logged out status <br>// <br><br>之前加: <br><br>ob_start(); <br>function replace_for_mod_rewrite(&$s) <br>{ <br>$urlin = <br>array( <br>"'(?"'(?"'(?"'(?"'(?"'(?"'(?"'(?"'(?"'(?"'(?); <br>$urlout = array( <br>"viewforum\\1-\\2-\\3.html", <br>"forum\\1.html", <br>"forum\\1.html", <br>"ptopic\\1.html", <br>"ntopic\\1.html", <br>"ftopic\\1-\\2-\\3-\\4.html", <br>"ftopic\\1.html", <br>"ftopic\\1-\\2.html", <br>"ftopic\\1.html", <br>"sutra\\1.html", <br>"sutra\\1.html", <br>); <br>$s = preg_replace($urlin, $urlout, $s); <br>return $s; <br>} <br><br>打开/includes/page_tail.php文件, <br><br>搜索代码: <br><br>$db->sql_close(); <br><br>之后加: <br><br>$contents = ob_get_contents(); <br>ob_end_clean(); <br>echo replace_for_mod_rewrite($contents); <br>global $dbg_starttime; <br><br>如果你的phpBB是2.06版本,打开includes/functions.php文件, <br><br>搜索代码: <br><br>if (!empty($db)) <br>{ <br>$db->sql_close(); <br>} <br><br>之后加: <br><br>if (stristr($url, 'http://')) { <br>header('Location: ' . $url); <br>exit; <br>} <br><br>最后在bbs目录下建立.htaccess 文件,文件内容为: <br><br>RewriteEngine On <br>RewriteRule ^forums.* index.php <br>RewriteRule ^forum([0-9]*).* viewforum.php?f=$1&mark=topic <br>RewriteRule ^viewforum([0-9]*)-([0-9]*)-([0-9]*).* viewforum.php?f=$1&topicdays=$2&start=$3 <br>RewriteRule ^forum([0-9]*).* viewforum.php?f=$1 <br>RewriteRule ^ptopic([0-9]*).* viewtopic.php?t=$1&view=previous <br>RewriteRule ^ntopic([0-9]*).* viewtopic.php?t=$1&view=next <br>RewriteRule ^ftopic([0-9]*)-([0-9]*)-([a-zA-Z]*)-([0-9]*).* viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4 <br>RewriteRule ^ftopic([0-9]*)-([0-9]*).* viewtopic.php?t=$1&start=$2 <br>RewriteRule ^ftopic([0-9]*).* viewtopic.php?t=$1 <br>RewriteRule ^ftopic([0-9]*).html viewtopic.php?t=$1&start=$2&postdays=$3&postorder=$4&highlight=$5 <br>RewriteRule ^sutra([0-9]*).* viewtopic.php?p=$1 <br><br>如果你的服务器不支持.htaccess,请打开httpd.conf文件,编辑你的虚拟主机部分,如下: <br><br><virtualhost> <br>ServerAdmin <font color="#333366">webmaster@domain.com</font> <br>DocumentRoot /home1/ftp/trotter/www <br>ServerName <font color="#333366">www.gbunix.com</font> <br>RewriteEngine On <br>RewriteRule ^/bbs/forums.* /bbs/index.php <br>RewriteRule ^/bbs/forum([0-9]*).* /bbs/viewforum.php?f=$1&mark=topic <br>RewriteRule ^/bbs/viewforum([0-9]*)-([0-9]*)-([0-9]*).* /bbs/viewforum.php?f=$1&topicdays=$2&start=$3 <br>RewriteRule ^/bbs/forum([0-9]*).* /bbs/viewforum.php?f=$1 <br>RewriteRule ^/bbs/ptopic([0-9]*).* /bbs/viewtopic.php?t=$1&view=previous <br>RewriteRule ^/bbs/ntopic([0-9]*).* /bbs/viewtopic.php?t=$1&view=next <br>RewriteRule ^/bbs/ftopic([0-9]*)-([0-9]*)-([a-zA-Z]*)-([0-9]*).* /bbs/viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4 <br>RewriteRule ^/bbs/ftopic([0-9]*)-([0-9]*).* /bbs/viewtopic.php?t=$1&start=$2 <br>RewriteRule ^/bbs/ftopic([0-9]*).* /bbs/viewtopic.php?t=$1 <br>RewriteRule ^/bbs/ftopic([0-9]*).html /bbs/viewtopic.php?t=$1&start=$2&postdays=$3&postorder=$4&highlight=$5 <br>RewriteRule ^/bbs/sutra([0-9]*).* /bbs/viewtopic.php?p=$1 <br>ErrorLog logs/gbunix.com-error_log <br>CustomLog logs/gbunix.com-access_log combined <br></virtualhost> <br><br>如果你用的不是虚拟主机,将RewriteRule部分代码放到httpd.conf文件最后就可以。 <br><br>注意:非常重要的一点,为了系统的安全,请在bbs发布目录下建立robots.txt文件,文件内容如下: <br><br>Disallow: /your-forum-folder/sutra*.html$ <br>Disallow: /your-forum-folder/ptopic*.html$ <br>Disallow: /your-forum-folder/ntopic*.html$ <br>Disallow: /your-forum-folder/ftopic*asc*.html$ <br><br>给apache安装mod_rewrite模块 <br><br> 如果你的服务器apache还没有安装,那很简单,在编译apache时将mod_rewrite模块编译进去就可以,相关文档可以在www.gbunix.com中找到。如果你的apache已经安装好了,现在只想编译出mod_rewrite.so模块,在apache中进行加载,下面我们就介绍这个方法。 <br><br>以Solaris操作系统进行举例: <br><br># PATH=/usr/local/bin:/usr/sfw/bin:/usr/ccs/bin:$PATH <br># export PATH <br># which gcc <br># which make <br><br># find ./ -name mod_rewrite.c //在apache的安装目录中寻找mod_rewrite.c文件 <br># cd PATH/to/mod_rewrite.c //进入包含mod_rewrite.c文件的目录 <br># apxs -c mod_foo.c //apxs请指定绝对路径,在你当前正在使用apache的bin目录里 <br># apxs -i -a -n mod_rewrite mod_rewrite.la <br><br>如果没有什么错误的话,应该在你的apache的modules目录中编译出一个mod_rewrite.so文件。 <br><br>编辑httpd.conf文件,确认httpd.conf中已经包含mod_rewrite.so的加载语句,如下: <br><br>LoadModule rewrite_module modules/mod_rewrite.so <br><br>这时,你的apache应该已经支持rewrite了。 <br><br>二.基于PATH_INFO技术实现: <br><br>修改phpBB代码: <br><br>打开overall_header.tpl文件,在首行加如下代码: <br><br><base href="http://www.your-forum.com/forum-dir/"> <br><br>打开config.php文件,在?>前加入如下代码: <br><br>if ($REQUEST_METHOD == "GET") { <br>if (strlen(getenv('PATH_INFO')) > 1) { <br>$GET_array = array(); <br>$PHP_SELF = str_replace(getenv('PATH_INFO'), '', $PHP_SELF); <br>$vars = explode('/', substr(getenv('PATH_INFO'), 1)); <br>for ($i=0, $n=sizeof($vars); $iif (strpos($vars[$i], '[]')) { <br>$GET_array[substr($vars[$i], 0, -2)][] = $vars[$i+1]; <br>} else { <br>$HTTP_GET_VARS[$vars[$i]] = $vars[$i+1]; <br>} <br>$i++; <br>} <br>if (sizeof($GET_array) > 0) { <br>while (list($key, $value) = each($GET_array)) { <br>$HTTP_GET_VARS[$key] = $value; <br>} <br>} <br>} <br><br>} <br><br>if ($REQUEST_METHOD == "POST") { <br>if (strlen(getenv('PATH_INFO')) > 1) { <br>$POST_array = array(); <br>$PHP_SELF = str_replace(getenv('PATH_INFO'), '', $PHP_SELF); <br>$vars = explode('/', substr(getenv('PATH_INFO'), 1)); <br>for ($i=0, $n=sizeof($vars); $iif (strpos($vars[$i], '[]')) { <br>$POST_array[substr($vars[$i], 0, -2)][] = $vars[$i+1]; <br>} else { <br>$HTTP_POST_VARS[$vars[$i]] = $vars[$i+1]; <br>} <br>$i++; <br>} <br>if (sizeof($GET_array) > 0) { <br>while (list($key, $value) = each($POST_array)) { <br>$HTTP_POST_VARS[$key] = $value; <br>} <br>} <br>} <br><br>} <br><br>打开functions.php文件,在?>前加入如下代码: <br><br>function replace_for_mod_rewrite($s) { <br><br>$s = str_replace("?", "/", $s); <br>$s = str_replace("&", "/", $s); <br>$s = str_replace("&", "/", $s); <br>$s = str_replace("=", "/", $s); <br>return $s; <br><br>} <br><br>打开sessions.php文件,用下面代码替换原来定义的append_sid()函数: <br><br>function append_sid($url, $non_html_amp = false) <br>{ <br>global $SID; <br><br><br>if ( !empty($SID) && !preg_match('#sid=#', $url) && !preg_match('#sid/#', $url) && !stristr( $_SERVER["HTTP_USER_AGENT"] ,'bot') && !stristr($_SERVER["HTTP_USER_AGENT"] ,'inktomi')) <br>{ <br>$url .= ( ( strpos($url, '?') != false ) ? ( ( $non_html_amp ) ? '&' : '&' ) : '?' ) . $SID ; <br>} <br>$url=replace_for_mod_rewrite($url); <br>return $url; <br>} <br><br>这时,你的论坛URL将会映射成(http://www.domain/bbs/viewtopic.php/t/4)这种方式。</span> <p style="width:100%;text-align:center;margin:10px 0"> <br> <br> </p> <p style="width:100%;text-align:center;margin:10px 0"> </p> <p class="clear"></p>