Apache 特有的 rewrite 技術很好用,Vovo2000.Com 的
/gallery/
/vovo-gallery/
/f/
/linka/
/paintbbs/ paintbbs-archive
...
都已經徹底嵌入 rewrite。
Rewrite 很簡單的說,就是把類似
==> /f/viewtopic.php?t=1388&start=0&sort=0
動態網頁,讓他給 user 的感覺是
==> /f/viewtopic-1388.html
而,實際上還是執行 viewtopic-1388.html&start=0&sort=0
再舉一個例子:
使用者看到 => /paintbbs/pch-viewer-1234.html
實際上執行 => /paintbbs/pch-viewer.cgi?pic=1234.png&pch=1234.pch
對 User 來說他只需要記得
Viewtopic-1388.html 或 pch-viewer-1234.html 即可
好處有:
1. URL 短一點,對使用者而言,靜態網址的接受度較高。
2. Search Engine Robots/Spiders/Crawlers 比較吃的下去
3. (未經證實)靜態網址的 SEO/SERP 效果好
4. 減少您 Log File / header流量的負擔,積少成多
要注意:
1. 要注意 Session ID 的控制或 Cache-Control 等議題
2. 一些使用者專屬個人資料的頁面,rewrite 時要注意。
3. .htaccess 的設定
===========================================
===========================================
在這裡舉 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. 接下來當然要建立 .htaccess 的 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;
再分享一下 Linka 的 rewrite .htaccess
代碼:<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /linka/
RewriteRule ^category/([0-9]+)\.html$ https://vovo2000.com/linka/links.php?cat=$1 [QSA]
RewriteRule ^action/([a-z0-9]+)\.html$ https://vovo2000.com/linka/links.php?action=$1 [QSA]
RewriteRule ^view/([a-z0-9]+)/([0-9]+)\.html$ https://vovo2000.com/linka/links.php?action=$1&link_id=$2 [QSA]
RewriteRule ^adminvlink/$ https://vovo2000.com/linka/ [R]
</IfModule>
供各位參考,善用 Rewrite 讓您的動態網站更美觀、更一致、更方便被「吃」 。 ________________
美術插畫設計案子報價系統 v0.1 Beta
爪哇禾雀
|