|
作者:Trotter 郵箱:trotter@kekerde.net 出處:www. 轉(zhuǎn)載請保持文檔完整,注明出處。 前言 隨著互聯(lián)網(wǎng)上的內(nèi)容以驚人速度的增長也越來越突出了搜索引擎的重要性,如果網(wǎng)站想更好地被搜索引擎收錄,網(wǎng)站設(shè)計除了面向用戶友好(User Friendly)外,搜索引擎友好(Search Engine Friendly)的設(shè)計也是非常重要的。進入搜索引擎的頁面內(nèi)容越多,則被用戶用不同的關(guān)鍵詞找到的幾率越大。不得不承認,將動態(tài)網(wǎng)頁鏈接rewriting成靜態(tài)鏈接是最保險和穩(wěn)定的面向搜索引擎優(yōu)化方式。該方案就是針對phpBB論壇系統(tǒng)的URL重定向提出的。 解決方案 URL重定向從技術(shù)上將,目前可以通過兩種方式實現(xiàn),一種是基于URL rewrite,另一種是基于PATH_INFO。例如http://www./bbs/ftopic102.html就是基于rewrite實現(xiàn)的,而http://www./article/article.php/515是基于PATH_INFO實現(xiàn)的。 針對PHPBB論壇的改造,我們分別就這兩種技術(shù)分別介紹。 一.使用rewrite技術(shù)實現(xiàn): 修改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 文件,文件內(nèi)容為: 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 如果你的服務(wù)器不支持.htaccess,請打開httpd.conf文件,編輯你的虛擬主機部分,如下: <VirtualHost 1.2.3.4> ServerAdmin webmaster@domain.com DocumentRoot /home1/ftp/trotter/www ServerName www. 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/-error_log CustomLog logs/-access_log combined </VirtualHost> 如果你用的不是虛擬主機,將RewriteRule部分代碼放到httpd.conf文件最后就可以。 注意:非常重要的一點,為了系統(tǒng)的安全,請在bbs發(fā)布目錄下建立robots.txt文件,文件內(nèi)容如下: 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模塊 如果你的服務(wù)器apache還沒有安裝,那很簡單,在編譯apache時將mod_rewrite模塊編譯進去就可以,相關(guān)文檔可以在www.中找到。如果你的apache已經(jīng)安裝好了,現(xiàn)在只想編譯出mod_rewrite.so模塊,在apache中進行加載,下面我們就介紹這個方法。 以Solaris操作系統(tǒng)進行舉例: # 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請指定絕對路徑,在你當(dāng)前正在使用apache的bin目錄里 # apxs -i -a -n mod_rewrite mod_rewrite.la 如果沒有什么錯誤的話,應(yīng)該在你的apache的modules目錄中編譯出一個mod_rewrite.so文件。 編輯httpd.conf文件,確認httpd.conf中已經(jīng)包含mod_rewrite.so的加載語句,如下: LoadModule rewrite_module modules/mod_rewrite.so 這時,你的apache應(yīng)該已經(jīng)支持rewrite了。 二.基于PATH_INFO技術(shù)實現(xiàn): 修改phpBB代碼: 打開overall_header.tpl文件,在首行加如下代碼: <base href="http://www./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()函數(shù): 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/viewtopic.php?t=199008 http://www./phpBB/viewtopic.php?t=137334 |
|
|