bo-blog WIN主机URL-Rewrite优化

      迫于一些要求,最近脱离了MHT,搬迁至新空间。由于该虚拟主机为WIN,原有bo-blog自带的.htaccess 规则已经无效,造成原自定义网址别名统统失效,可惜各搜索引擎均收录了这些别名地址。
    Bo-Blog官方网站上有一些处理方法,但只能实现类似http://www.pandaking.cn/post/493/或http://www.pandaking.cn/post/Aircraft-painted-for-Asian-Games/类似的优化,原来的自定义别名地址可是没有/POST/的(http://www.pandaking.cn/Aircraft-painted-for-Asian-Games/),引擎上的旧地址统统都将无效。
    Panda将官网上的解决方法予以融合,出现了想不到的效果。
    有兴趣的继续往下看:

====
第一步
====
将下面代码复制生成httpd.ini文件,并上传到网站根目录。

[ISAPI_Rewrite]
CacheClockRate 3600
RepeatLimit 32

RewriteRule ^(.*)/post/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ $1/read\.php\?entryid=$2&page=$3&part=$4 [I]
RewriteRule ^(.*)/page/([0-9]+)/([0-9]+)/?$ $1/index\.php\?mode=$2&page=$3 [I]
RewriteRule ^(.*)/starred/([0-9]+)/?([0-9]+)?/?$ $1/star\.php\?mode=$2&page=$3 [I]
RewriteRule ^(.*)/category/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ $1/index\.php\?go=category_$2&mode=$3&page=$4 [I]
RewriteRule ^(.*)/archiver/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ $1/index\.php\?go=archive&cm=$2&cy=$3&mode=$4&page=$5 [I]
RewriteRule ^(.*)/date/([0-9]+)/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ $1/index\.php\?go=showday_$2-$3-$4&mode=$5&page=$6 [I]
RewriteRule ^(.*)/user/([0-9]+)/?$ $1/view\.php\?go=user_$2 [I]
RewriteRule ^(.*)/tags/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ $1/tag\.php\?tag=$2&mode=$3&page=$4 [I]
RewriteRule ^(.*)/component/id/([0-9]+)/?$ $1/page\.php\?pageid=$2 [I]
RewriteRule ^(.*)/component/([^/]+)/?$ $1/page\.php\?pagealias=$2 [I]

#Customized URL
RewriteRule ^(.*)/post/([^/]*)\_?([0-9]+)?\_?([0-9]+)?/?$ $1/read\.php\?blogalias=$2&page=$3&part=$4 [I]

#Force redirection for old rules
RewriteRule ^(.*)/post/([0-9]+)\.htm$ $1/post/$2/ [R]
RewriteRule ^(.*)/post/([0-9]+)\_([0-9]+)\.htm$ $1/post/$2/$3/ [R]
RewriteRule ^(.*)/post/([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ $1/post/$2/$3/$4/ [R]
RewriteRule ^(.*)/index\_([0-9]+)\_([0-9]+)\.htm$ $1/page/$2/$3/ [R]
RewriteRule ^(.*)/star\_([0-9]+)\_([0-9]+)\.htm$ $1/starred/$2/$3/ [R]
RewriteRule ^(.*)/category\_([0-9]+)\.htm$ $1/category/$2/ [R]
RewriteRule ^(.*)/category\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ $1/category/$2/$3/$4/ [R]
RewriteRule ^(.*)/archive\_([0-9]+)\_([0-9]+)\.htm$ $1/archiver/$2/$3/ [R]
RewriteRule ^(.*)/archive\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ $1/archiver/$2/$3/$4/$5/ [R]
RewriteRule ^(.*)/showday\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ $1/date/$2/$3/$4/ [R]
RewriteRule ^(.*)/showday\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ $1/date/$2/$3/$4/$5/$6/ [R]

做到这一步,就可以启用高级URL优化了,但是和原来的相比,在链接中多了/POST/,本以为将代码中的#Customized URL中的post去掉,就可以了,实际证明是不可以的。
所以需要继续做下一步:
=====
第二步
=====
将下面的代码,复制为404.php,上传到网站根目录
注意:如果博客目录不是根目录,则需要修改$blogdir='';为针对的目录

$blogdir='';

//========================Rules=========================//

$RewriteRules=$RedirectTo=$ClearRewrite=$NewRedirect=array();

$blogdirp=preg_quote($blogdir, '/');

$RewriteRules[]=”/{$blogdirp}post\/([0-9]+)\/?([0-9]+)?\/?([0-9]+)?\/?/”;
$RewriteRules[]=”/{$blogdirp}page\/([0-9]+)\/([0-9]+)\/?/”;
$RewriteRules[]=”/{$blogdirp}starred\/([0-9]+)\/?([0-9]+)?\/?/”;
$RewriteRules[]=”/{$blogdirp}category\/([^\/|\.]*)\/?([0-9]+)?\/?([0-9]+)?\/?/”;
$RewriteRules[]=”/{$blogdirp}archiver\/([0-9]+)\/([0-9]+)\/?([0-9]+)?\/?([0-9]+)?\/?/”;
$RewriteRules[]=”/{$blogdirp}date\/([0-9]+)\/([0-9]+)\/([0-9]+)\/?([0-9]+)?\/?([0-9]+)?\/?/”;
$RewriteRules[]=”/{$blogdirp}user\/([0-9]+)\/?/”;
$RewriteRules[]=”/{$blogdirp}tags\/([^\/]+)\/?([0-9]+)?\/?([0-9]+)?\/?/”;
$RewriteRules[]=”/{$blogdirp}component\/id\/([0-9]+)\/?/”;
$RewriteRules[]=”/{$blogdirp}component\/([^\/]+)\/?/”;
$RewriteRules[]=”/{$blogdirp}([^\/|\.]*)\/?([0-9]+)?\/?([0-9]+)?\/?/”;

$RedirectTo[]=”{$blogdir}read.php?entryid=\\1&page=\\2&part=\\3″;
$RedirectTo[]=”{$blogdir}index.php?mode=\\1&page=\\2″;
$RedirectTo[]=”{$blogdir}star.php?mode=\\1&page=\\2″;
$RedirectTo[]=”{$blogdir}index.php?go=category_\\1&mode=\\2&page=\\3″;
$RedirectTo[]=”{$blogdir}index.php?go=archive&cm=\\1&cy=\\2&mode=\\3&page=\\4″;
$RedirectTo[]=”{$blogdir}index.php?go=showday_\\1-\\2-\\3&mode=\\4&page=\\5″;
$RedirectTo[]=”{$blogdir}view.php?go=user_\\1″;
$RedirectTo[]=”{$blogdir}tag.php?tag=\\1&mode=\\2&page=\\3″;
$RedirectTo[]=”{$blogdir}page.php?pageid=\\1″;
$RedirectTo[]=”{$blogdir}page.php?pagealias=\\1″;
$RedirectTo[]=”{$blogdir}read.php?blogalias=\\1&page=\\2&part=\\3″;

$ClearRewrite[]=”/{$blogdirp}post\/([0-9]+)\.htm/e”;
$ClearRewrite[]=”/{$blogdirp}post\/([0-9]+)_([0-9]+)\.htm/e”;
$ClearRewrite[]=”/{$blogdirp}post\/([0-9]+)_([0-9]+)_([0-9]+)\.htm/e”;
$ClearRewrite[]=”/{$blogdirp}index_([0-9]+)_([0-9]+)\.htm/e”;
$ClearRewrite[]=”/{$blogdirp}star_([0-9]+)_([0-9]+)\.htm/e”;
$ClearRewrite[]=”/{$blogdirp}category_([0-9]+)\.htm/e”;
$ClearRewrite[]=”/{$blogdirp}category_([0-9]+)_([0-9]+)_([0-9]+)\.htm/e”;
$ClearRewrite[]=”/{$blogdirp}archive_([0-9]+)_([0-9]+)\.htm/e”;
$ClearRewrite[]=”/{$blogdirp}archive_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)\.htm/e”;
$ClearRewrite[]=”/{$blogdirp}showday_([0-9]+)_([0-9]+)_([0-9]+)\.htm/e”;
$ClearRewrite[]=”/{$blogdirp}showday_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)\.htm/e”;
$ClearRewrite[]=”/{$blogdirp}([^\/]*)\.html/e”;

$NewRedirect[]=”generate_301('{$blogdir}post/\\1/')”;
$NewRedirect[]=”generate_301('{$blogdir}post/\\1/\\2/')”;
$NewRedirect[]=”generate_301('{$blogdir}post/\\1/\\2/\\3/')”;
$NewRedirect[]=”generate_301('{$blogdir}page/\\1/\\2/')”;
$NewRedirect[]=”generate_301('{$blogdir}starred/\\1/\\2/')”;
$NewRedirect[]=”generate_301('{$blogdir}category/\\1/')”;
$NewRedirect[]=”generate_301('{$blogdir}category/\\1/\\2/\\3/')”;
$NewRedirect[]=”generate_301('{$blogdir}archiver/\\1/\\2/')”;
$NewRedirect[]=”generate_301('{$blogdir}archiver/\\1/\\2/\\3/\\4/')”;
$NewRedirect[]=”generate_301('{$blogdir}date/\\1/\\2/\\3/')”;
$NewRedirect[]=”generate_301('{$blogdir}date/\\1/\\2/\\3/\\4/\\5/')”;
$NewRedirect[]=”generate_301('{$blogdir}\\1/')”;

//========================Execution=========================//

$q_url=$_SERVER[”REQUEST_URI”];
@list($none, $currentURL)=@explode(';', $q_url);
if (!$currentURL) generate_real_404();
if (strstr($currentURL, 'http://')) {
$forceURL=parse_url($currentURL);
if (substr($forceURL['path'], 0, 1)=='/') $currentURL=substr($forceURL['path'], 1);
}

$rewritedURL=preg_replace($ClearRewrite, $NewRedirect, $currentURL);

$i=0;
foreach ($RewriteRules as $rule) {
if (preg_match($rule, $rewritedURL)) {
$rewritedURL=preg_replace($rule, $RedirectTo[$i], $rewritedURL, 1);
break;
}
$i+=1;
}

if ($currentURL==$rewritedURL) generate_real_404();

$parsedURL=parse_url ($rewritedURL);

parse_str($parsedURL['query']);

chdir('./'.$blogdir);
include(basename($parsedURL['path']));

function generate_real_404 () {
@header (“HTTP/1.1 404 Not Found”);
die(“Not Found

HTTP/1.1 404 Not Found

“);
}

function generate_301 ($to) {
@header (“HTTP/1.1 301 Moved Permanently”);
@header (“Location: /{$to}”);
exit();
}

=====
第三步
=====
在bo-blog设置中“指定HTTP 404页面”中填写你刚才的404.php地址。
=====
第四步
=====
在虚拟主机中设置读取自定义404地址

over!
至此,旧地址可正常跳转到现地址,即:http://website/post/name和http://website/name可同时访问。

此方法可能有更好的替代方式。

You may also like...

6 Responses

  1. 21sta说道:

    求404.php文件

  2. 21sta说道:

    404.php复制后变全角。

  3. 甜可儿说道:

    伪静态规则我不太明白,我直接下载了博主的文件放到根目录,开启高级url优化,结果日志无法显示,这是怎么回事呢?

  4. 阅城说道:

    熊猫兄的博客终于恢复了,你的风格我很中意呢,那么久没看到还以为你销声匿迹了呢~我想给博客换程序来着,可是工程过大,另外还有些数据不能完全转换,手动改数据库的话很麻烦,所以想法也就夭折了~
    2010年到了,阅城祝你新的一年里心中事圆、外有财源、内有福源、广结人缘、左右逢源,新年快乐~!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

[em30] [em29] [em28] [em27] [em26] [em25] [em24] [em23] [em22] [em21] [em20] [em19] [em18] [em17] [em16] [em15] [em14] [em13] [em12] [em11] [em10] [em09] [em08] [em07] [em06] [em05] [em04] [em03] [em02] [em01]