如何把动态页面转化为静态页面(PHPBB MOD for Google 完全解决方案)

作者:Trotter
邮箱:trotter@kekerde.net
出处:http://www.gbunix.com/article/article.php/515

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

前言

  随着互联网上的内容以惊人速度的增长也越来越突出了搜索引擎的重要性,如果网站想更好地被搜索引擎收录,网站设计除了面向用户友好(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?f=$1
RewriteRule ^/bbs/ptopic([0-9]*).* /bbs/viewtopic.php?t=$1&view=previous
RewriteRule ^/bbs/ntopic([0-9]*).* /bbs/viewtopic.php?t=$1&view=next
RewriteRule ^/bbs/ftopic([0-9]*)-([0-9]*)-([a-zA-Z]*)-([0-9]*).* /bbs/viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4
RewriteRule ^/bbs/ftopic([0-9]*)-([0-9]*).* /bbs/viewtopic.php?t=$1&start=$2
RewriteRule ^/bbs/ftopic([0-9]*).* /bbs/viewtopic.php?t=$1
RewriteRule ^/bbs/ftopic([0-9]*).html /bbs/viewtopic.php?t=$1&start=$2&postdays=$3&postorder=$4&highlight=$5
RewriteRule ^/bbs/sutra([0-9]*).* /bbs/viewtopic.php?p=$1
ErrorLog logs/gbunix.com-error_log
CustomLog logs/gbunix.com-access_log combined
</VirtualHost>

如果你用的不是虚拟主机,将RewriteRule部分代码放到httpd.conf文件最后就可以。

注意:非常重要的一点,为了系统的安全,请在bbs发布目录下建立robots.txt文件,文件内容如下:

Disallow: /your-forum-folder/sutra*.html$
Disallow: /your-forum-folder/ptopic*.html$
Disallow: /your-forum-folder/ntopic*.html$
Disallow: /your-forum-folder/ftopic*asc*.html$

给apache安装mod_rewrite模块

  如果你的服务器apache还没有安装,那很简单,在编译apache时将mod_rewrite模块编译进去就可以,相关文档可以在www.gbunix.com中找到。如果你的apache已经安装好了,现在只想编译出mod_rewrite.so模块,在apache中进行加载,下面我们就介绍这个方法。

以Solaris操作系统进行举例:

# PATH=/usr/local/bin:/usr/sfw/bin:/usr/ccs/bin:$PATH
# export PATH
# which gcc
# which make

# find ./ -name mod_rewrite.c //在apache的安装目录中寻找mod_rewrite.c文件
# cd PATH/to/mod_rewrite.c //进入包含mod_rewrite.c文件的目录
# apxs -c mod_foo.c //apxs请指定绝对路径,在你当前正在使用apache的bin目录里
# apxs -i -a -n mod_rewrite mod_rewrite.la

如果没有什么错误的话,应该在你的apache的modules目录中编译出一个mod_rewrite.so文件。

编辑httpd.conf文件,确认httpd.conf中已经包含mod_rewrite.so的加载语句,如下:

LoadModule rewrite_module modules/mod_rewrite.so

这时,你的apache应该已经支持rewrite了。

二.基于PATH_INFO技术实现:

修改phpBB代码:

打开overall_header.tpl文件,在首行加如下代码:

<base href="http://www.your-forum.com/forum-dir/">

打开config.php文件,在?>前加入如下代码:

if ($REQUEST_METHOD == "GET") {
if (strlen(getenv('PATH_INFO')) > 1) {
$GET_array = array();
$PHP_SELF = str_replace(getenv('PATH_INFO'), '', $PHP_SELF);
$vars = explode('/', substr(getenv('PATH_INFO'), 1));
for ($i=0, $n=sizeof($vars); $i<$n; $i++) {
if (strpos($vars[$i], '[]')) {
$GET_array[substr($vars[$i], 0, -2)][] = $vars[$i+1];
} else {
$HTTP_GET_VARS[$vars[$i]] = $vars[$i+1];
}
$i++;
}
if (sizeof($GET_array) > 0) {
while (list($key, $value) = each($GET_array)) {
$HTTP_GET_VARS[$key] = $value;
}
}
}

}

if ($REQUEST_METHOD == "POST") {
if (strlen(getenv('PATH_INFO')) > 1) {
$POST_array = array();
$PHP_SELF = str_replace(getenv('PATH_INFO'), '', $PHP_SELF);
$vars = explode('/', substr(getenv('PATH_INFO'), 1));
for ($i=0, $n=sizeof($vars); $i<$n; $i++) {
if (strpos($vars[$i], '[]')) {
$POST_array[substr($vars[$i], 0, -2)][] = $vars[$i+1];
} else {
$HTTP_POST_VARS[$vars[$i]] = $vars[$i+1];
}
$i++;
}
if (sizeof($GET_array) > 0) {
while (list($key, $value) = each($POST_array)) {
$HTTP_POST_VARS[$key] = $value;
}
}
}

}

打开functions.php文件,在?>前加入如下代码:

function replace_for_mod_rewrite($s) {

$s = str_replace("?", "/", $s);
$s = str_replace("&", "/", $s);
$s = str_replace("&", "/", $s);
$s = str_replace("=", "/", $s);
return $s;

}

打开sessions.php文件,用下面代码替换原来定义的append_sid()函数:

function append_sid($url, $non_html_amp = false)
{
global $SID;


if ( !empty($SID) && !preg_match('#sid=#', $url) && !preg_match('#sid/#', $url) && !stristr( $_SERVER["HTTP_USER_AGENT"] ,'bot') && !stristr($_SERVER["HTTP_USER_AGENT"] ,'inktomi'))
{
$url .= ( ( strpos($url, '?') != false ) ? ( ( $non_html_amp ) ? '&' : '&' ) : '?' ) . $SID ;
}
$url=replace_for_mod_rewrite($url);
return $url;
}

这时,你的论坛URL将会映射成(http://www.domain/bbs/viewtopic.php/t/4)这种方式。

参考文献:

http://www.phpbb.com/phpBB/viewtopic.php?t=199008

http://www.phpbb.com/phpBB/viewtopic.php?t=137334

##################################################################################### ## ## MOD Title: AUCTION MOD 1.3m ## MOD Author: FR (www.phpbb-auction.com) ## MOD Description: - ## MOD Version: 1.3m ## ## Installation Level: (Middle) ## Installation Time: till your done ## ## Requirements: A running phpBB 2.0.x (lastest version recommended) ## GD 1.8.x - 2.0 or higher (require for auto-thumbnail ## HTTP File Upload Enabled ## ## Tested with: __________ ## ## Files To Edit: viewonline.php ## viewtopic.php ## admin/index.php ## includes/usercp_viewprofile.php ## includes/page_header.php ## language/lang_english/lang_admin.php ## language/lang_english/lang_main.php ## templates/subSilver/subSilver.cfg ## templates/subSilver/overall_header.tpl ## templates/subSilver/viewtopic_body.tpl ## templates/subSilver/profile_view_body.tpl ## ## Included Files: to much to mention ## ##################################################################################### ## ## Before Adding This MOD To Your Forum, You Should Back Up All Files ## Related To This MOD ## ##################################################################################### ## ## For Security Purposes, you should check www.phpbb-auction.com for news and patches ## ##################################################################################### ## ## Author Notes: ## ## ##################################################################################### ## ## This hack is released under the GPL License. ## This hack can be freely used, but not distributed, without permission. ## Intellectual Property is retained by the author listed above. ## ##################################################################################### # # If you have the phpbb-auction 1.2m installed you should use the update.txt # document. # # #-----[ COPY ]------------------------------------------------------- # # Upload all files in directory "phpbb_root" with their structure to your # phpBB root directory # # Remember to upload all the language files and template files to all your # language packs and template directories # # If you use FTP please remember to use ASCII mode for text files (*.php, *.tpl) # and BINARY mode for image files (*.jpg, *.gif) # Fortunately good FTP clients today can auto-detect the mode for your files # #-----[ ACTION ]-------------------------------------- # Require for Unix-like host (you can use your FTP client to do this) # # CHMOD 777 auction/upload/ # CHMOD 777 auction/upload/cache/ # CHMOD 777 auction/upload/main/ # CHMOD 777 auction/upload/main/watermark/ # CHMOD 777 auction/upload/mini/ # CHMOD 777 auction/upload/tmp/ # CHMOD 777 auction/upload/wmk/ # CHMOD 777 auction/upload/watermark/ # CHMOD 777 auction/upload/wmk/main_watermark.png # CHMOD 777 auction/upload/wmk/big_watermark.png # #-----[ ACTION ]-------------------------------------- # # Run the install_db.php and delete (!!!) the file afterwards //////////////////////////////////////////////////////////////////////////////// 3 - Perform the following filechanges //////////////////////////////////////////////////////////////////////////////// # #-----[ OPEN ]------------------------------------------ # viewonline.php # #-----[ FIND ]------------------------------------------ # include($phpbb_root_path . 'includes/page_header.'.$phpEx); # #-----[ AFTER, ADD ]------------------------------------------ # include($phpbb_root_path . 'auction/auction_common.'.$phpEx); # #-----[ FIND ]------------------------------------------ # case PAGE_FAQ: $location = $lang['Viewing_FAQ']; $location_url = "faq.$phpEx"; break; # #-----[ AFTER, ADD ]------------------------------------------ # case AUCTION_ROOM: $location = $lang['Auction']; $location_url = "auction.$phpEx"; break; case AUCTION_OFFER: $location = $lang['Auction']; $location_url = "auction.$phpEx"; break; case AUCTION_RATING: $location = $lang['Auction']; $location_url = "auction.$phpEx"; break; case AUCTION_FAQ: $location = $lang['Auction']; $location_url = "auction.$phpEx"; break; case AUCTION_MYAUCTION: $location = $lang['Auction']; $location_url = "auction.$phpEx"; break; case AUCTION_OFFER_VIEW: $location = $lang['Auction']; $location_url = "auction.$phpEx"; break; case AUCTION_SITEMAP: $location = $lang['Auction']; $location_url = "auction.$phpEx"; break; case AUCTION_PIC_MANAGER: $location = $lang['Auction']; $location_url = "auction.$phpEx"; break; # #-----[ OPEN ]------------------------------------------ # admin/index.php # #-----[ FIND ]------------------------------------------ # require('./pagestart.' . $phpEx); # #-----[ AFTER, ADD ]------------------------------------------ # include($phpbb_root_path . 'auction/auction_common.'.$phpEx); # #-----[ FIND ]------------------------------------------ # # 2 times case PAGE_FAQ: $location = $lang['Viewing_FAQ']; $location_url = "index.$phpEx?pane=right"; break; # #-----[ AFTER, ADD ]------------------------------------------ # # 2 times case AUCTION_ROOM: $location = $lang['Auction']; $location_url = "auction.$phpEx?pane=right"; break; case AUCTION_OFFER: $location = $lang['Auction']; $location_url = "auction.$phpEx?pane=right"; break; case AUCTION_RATING: $location = $lang['Auction']; $location_url = "auction.$phpEx?pane=right"; break; case AUCTION_FAQ: $location = $lang['Auction']; $location_url = "auction.$phpEx?pane=right"; break; case AUCTION_MYAUCTION: $location = $lang['Auction']; $location_url = "auction.$phpEx?pane=right"; break; case AUCTION_OFFER_VIEW: $location = $lang['Auction']; $location_url = "auction.$phpEx?pane=right"; break; case AUCTION_SITEMAP: $location = $lang['Auction']; $location_url = "auction.$phpEx?pane=right"; break; case AUCTION_PIC_MANAGER: $location = $lang['Auction']; $location_url = "auction.$phpEx?pane=right"; break; # #-----[ OPEN ]------------------------------------------ # /language/lang_english/lang_admin.php # #-----[ FIND ]------------------------------------------ # $lang['Styles'] = 'Styles Admin'; # #-----[ AFTER, ADD ]------------------------------------------ # $lang['Auction'] = 'Auction'; $lang['a1_configuration'] = 'Configuration'; $lang['a2_roommangement'] = 'Manage Rooms'; $lang['a4_coupons'] = 'Manage Coupons'; $lang['a3_offer'] = 'Manage Offers'; $lang['a5_ipn_log'] = 'Manage IPN Log'; $lang['a6_picture_Configuration'] = 'Manage Images'; $lang['a7_permission'] = 'Manage Permissions'; $lang['a8_account'] = 'Manage Account'; # #-----[ OPEN ]------------------------------------------ # /templates/subSilver/subSilver.cfg # #-----[ FIND ]------------------------------------------ # $current_template_images = $current_template_path . "/images"; # #-----[ AFTER, ADD ]------------------------------------------ # $images['auction_locked']="$current_template_images/auction_locked.gif"; $images['auction_open']="$current_template_images/auction_open.gif"; $images['icon_auction_delete']="$current_template_images/auction_delete.gif"; $images['icon_auction_move']="$current_template_images/auction_move.gif"; $images['icon_auction_pic']="$current_template_images/auction_pic.gif"; $images['icon_auction_no_pic']="$current_template_images/auction_nopic.gif"; $images['icon_auction_down']="$current_template_images/auction_down.gif"; $images['icon_auction_up']="$current_template_images/auction_up.gif"; $images['icon_auction_feature']="$current_template_images/auction_feature.gif"; $images['icon_rating1']="$current_template_images/rating/1.gif"; $images['icon_rating2']="$current_template_images/rating/2.gif"; $images['icon_rating3']="$current_template_images/rating/3.gif"; $images['icon_rating4']="$current_template_images/rating/4.gif"; $images['icon_auction_watch']="$current_template_images/auction_add.gif"; $images['icon_auction_user_rating'] = "$current_template_images/{LANG}/icon_auction_user_rating.gif"; $images['newoffer'] = "$current_template_images/{LANG}/newoffer.gif"; $images['direct_sell'] = "$current_template_images/{LANG}/auction_buy_now.gif"; $images['vote_left'] = "$current_template_images/vote_lcap.gif"; $images['vote_right'] = "$current_template_images/vote_rcap.gif"; $images['auction_vote_right'] = "$current_template_images/auction_voting_barb.gif"; $images['auction_vote'] = "$current_template_images/auction_voting_bar.gif"; # #-----[ OPEN ]------------------------------------------ # /language/lang_english/lang_main.php # #-----[ FIND ]------------------------------------------ # $lang['datetime']['Dec'] = 'Dec'; # #-----[ AFTER, ADD ]------------------------------------------ # // Auction $lang['Auction'] = 'Auction'; # #-----[ OPEN ]------------------------------------------ # /templates/subSilver/overall_header.tpl # #-----[ FIND ]------------------------------------------ # <a href="{U_GROUP_CP}" class="mainmenu"><img src="templates/subSilver/images/icon_mini_groups.gif" width="12" height="13" border="0" alt="{L_USERGROUPS}" hspace="3" />{L_USERGROUPS}</a> # #-----[ AFTER, ADD ]------------------------------------------ #   <a href="{U_AUCTION}" class="mainmenu"><img src="templates/subSilver/images/icon_mini_auction.gif" width="12" height="13" border="0" alt="{L_AUCTION}" hspace="3" />{L_AUCTION}</a> # #-----[ OPEN ]------------------------------------------ # includes/page_header.php # #-----[ FIND ]------------------------------------------ # 'L_FAQ' => $lang['FAQ'], # #-----[ AFTER, ADD ]------------------------------------------ # 'L_AUCTION' => $lang['Auction'], # #-----[ FIND ]------------------------------------------ # 'U_FAQ' => append_sid('faq.'.$phpEx), # #-----[ AFTER, ADD ]------------------------------------------ # 'U_AUCTION' => append_sid('auction.'.$phpEx), # #-----[ OPEN ]------------------------------------------ # viewtopic.php # #-----[ FIND ]------------------------------------------ # include($phpbb_root_path . 'includes/bbcode.'.$phpEx); # #-----[ AFTER, ADD ]------------------------------------------ # // Start Include language file $language = $board_config['default_lang']; if( !file_exists($phpbb_root_path . 'language/lang_' . $language . '/lang_auction.'.$phpEx) ) { $language = 'english'; } include($phpbb_root_path . 'language/lang_' . $language . '/lang_auction.' . $phpEx); // end include language file # #-----[ FIND ]------------------------------------------ # 'L_MINI_POST_ALT' => $mini_post_alt, # #-----[ AFTER, ADD ]------------------------------------------ # 'AUCTION_USER_RATING' => "<a href=" . append_sid("auction_rating.php?mode=view&" . POST_USERS_URL . "=" .$poster_id ) . "><img src=" . $images['icon_auction_user_rating'] . " alt='Feedback Rating' border='0' /></a>", 'L_VIEW_AUCTION_USER_RATING' => $lang['auction_user_rating'], # #-----[ OPEN ]------------------------------------------ # includes/usercp_viewprofile.php # #-----[ FIND ]------------------------------------------ # if ( !defined('IN_PHPBB') ) { die("Hacking attempt"); exit; } # #-----[ AFTER, ADD ]------------------------------------------ # // Start Include language file $language = $board_config['default_lang']; if( !file_exists($phpbb_root_path . 'language/lang_' . $language . '/lang_auction.'.$phpEx) ) { $language = 'english'; } include($phpbb_root_path . 'language/lang_' . $language . '/lang_auction.' . $phpEx); // end include language file # #-----[ FIND ]------------------------------------------ # 'AVATAR_IMG' => $avatar_img, # #-----[ AFTER, ADD ]------------------------------------------ # 'AUCTION_USER_RATING' => "<a href=\"" . append_sid("auction_rating.php?mode=view&" . POST_USERS_URL . "=" . $profiledata['user_id'] ) . "\"><img src=\"" . $images['icon_auction_user_rating'] . "\" alt=\"" . $lang['auction_user_rating'] . "\" title=\"" . $lang['auction_user_rating'] . "\" border=\"0\" /></a>", 'L_VIEW_AUCTION_USER_RATING' => $lang['auction_user_rating'], # #-----[ OPEN ]------------------------------------------ # /templates/subSilver/viewtopic_body.tpl # #-----[ FIND ]------------------------------------------ # {postrow.MSN_IMG} # #-----[ AFTER,ADD ]------------------------------------------ # // Goes in same line before {postrow.MSN_IMG} {postrow.AUCTION_USER_RATING} # #-----[ OPEN ]------------------------------------------ # /templates/subSilver/profile_view_body.tpl # #-----[ FIND ]------------------------------------------ # <tr> <td valign="middle" nowrap="nowrap" align="right"><span class="gen">{L_AIM}:</span></td> <td class="row1" valign="middle"><span class="gen">{AIM_IMG}</span></td> </tr> # #-----[ AFTER, ADD ]------------------------------------------ # <tr> <td valign="middle" nowrap="nowrap" align="right"><span class="gen">{L_VIEW_AUCTION_USER_RATING}:</span></td> <td class="row1" valign="middle"><span class="gen">{AUCTION_USER_RATING}</span></td> </tr> # #-----[ SAVE/CLOSE ALL FILES ]------------------------------------------ # ##################################################################################### ## Further Information ## ## Thats it! You are through. If you have any questions dont hesitate to use the ## Support Forums on www.phpbb-auction.com. If you want to use the payment methods ## you should read the documentation for it on the projects website. ## Greetings and have fun with phpbb-Auction ## FR ## EoM #####################################################################################
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值