原作者:inso 藕是一头猪 By : inso
来自:http://www.shi8.com/255.html
--------------------------------------------------------------------------------
Apache 特有的 rewrite 技術很好用,Vovo2000.Com 的
/gallery/
/vovo-gallery/
/phpbb2/
/linka/
...
都已經徹底嵌入 rewrite。
Rewrite 很簡單的說,就是把類似
==> /phpbb2/viewtopic.php?t=1388&start=0
動態網頁,轉成
==> /phpbb2/viewtopic-1388-0.html
好處有:
1. URL 短一點,靜態網址的接受度較高。
2. Search Engine 比較吃的下去~
3. 靜態的網址會有比較好的 SE-BENCHMARK
===========================================
===========================================
在這裡舉 PHPBB2 2.0.3 的例子來說,一步一步來:
1. 把 Apache 的 Rewrite 打開(mod_rewrite)
2. 在 phpbb/include/page_header.php
的 // Generate logged in/logged out status 之前加入
代碼:
ob_start();
function replace_for_mod_rewrite(&$s)
{
$urlin = array(
"'(?<!/)viewforum.php?f=([a-zA-Z0-9]+)&topicdays=([0-9]*)&start=([0-9]*)'",
"'(?<!/)viewforum.php?f=([a-zA-Z0-9]+)&mark=topics'",
"'(?<!/)viewforum.php?f=([a-zA-Z0-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]*)'",
"'(?<!/)viewtopic.php?t=([0-9]+)'",
"'(?<!/)viewtopic.php?p=([0-9]+)'",
"'(?<!/)profile.php?mode=viewprofile&u=([0-9]+)'"
);
$urlout = array(
"viewforum-$1-$2-$3.html",
"forum-$1.html",
"forum-$1.html",
"prev-topic-$1.html",
"next-topic-$1.html",
"viewtopic-$1-$2-$3-$4.html",
"viewtopic-$1-$2.html",
"viewtopic-$1.html",
"viewpost-$1.html",
"profile-$1.html"
);
$s = preg_replace($urlin, $urlout, $s);
$s = mb_ereg_replace( "viewtopic-([0-9]+).html&(.*)$", "viewtopic.php?t=/1&/2", $s);
return $s;
}
3. 在 phpbb/include/page_tail.php
代碼:
在下列?#123;式之後:
$db->sql_close();
?#91;入:
$contents = ob_get_contents();
ob_end_clean();
echo replace_for_mod_rewrite($contents);
global $dbg_starttime;
在下列?#123;式之後:
ob_end_clean();
?#91;入:
echo replace_for_mod_rewrite($contents);
global $dbg_starttime;
4. 接下來當然要建立 .htacce 的 rewrite rule
代碼:
RewriteEngine On
RewriteRule ^viewforum-([0-9a-zA-Z]+)-([0-9]+)-([0-9]+).html$ viewforum.php?f=$1&topicdays=$2&start=$3
RewriteRule ^forum-([0-9a-zA-Z]+).html$ viewforum.php?f=$1&mark=topic
RewriteRule ^forum-([0-9a-zA-Z]+).html$ viewforum.php?f=$1
RewriteRule ^prev-topic-([0-9]+).html$ viewtopic.php?t=$1&view=previous
RewriteRule ^next-topic-([0-9]+).html$ viewtopic.php?t=$1&view=next
RewriteRule ^viewtopic-([0-9]+)-([0-9]+).html$ viewtopic.php?t=$1&start=$2
RewriteRule ^viewtopic-([0-9]+)-([0-9]+)-([a-zA-Z]*)-([0-9]+).html$ viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4
RewriteRule ^viewtopic-([0-9]+).html$ viewtopic.php?t=$1
RewriteRule ^viewpost-([0-9]+).html$ viewtopic.php?p=$1
RewriteRule ^viewtopic-([0-9]+).html&highlight=(.*)$ viewtopic.php?t=$1&highlight=$2
RewriteRule ^profile-([0-9]+).html$ profile.php?mode=viewprofile&u=$1
5. 測試~ 當然 OK;
供各位參考,善用 Rewrite 讓您的動態網站更美觀、更一致、更方便被「吃」 。
phpbb的假静态化方法 Apache Rewrite 技術
最新推荐文章于 2020-08-10 02:47:38 发布