eygle.com   eygle.com
eygle.com eygle
eygle.com  
 

« Apache 重写规则的常见应用 (rewrite) | 文摘首页 | TOM在线拉动中国网络股上涨 网易突破90美元 »

Apache Rewrite 技术
modb.pro

原文链接

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(
"'(? "'(? "'(? "'(? "'(? "'(? "'(? "'(? "'(? "'(? );
$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 让您的动态网站更美观、更一致、更方便被「吃」 。


历史上的今天...
    >> 2010-09-25文章:
    >> 2009-09-25文章:
           IT人物之 - 惠普CEO Mark Hurd

By eygle on 2005-09-25 11:19 | Comments (0) | 学习资料 | 1120 |


CopyRight © 2004~2020 云和恩墨,成就未来!, All rights reserved.
数据恢复·紧急救援·性能优化 云和恩墨 24x7 热线电话:400-600-8755 业务咨询:010-59007017-7040 or 7037 业务合作: marketing@enmotech.com