bo-blog在不支持rewrite的主机中使用高级url的方法

高级Url优化有助于SEO,但在许多windows主机中无法使用 .htaccess 的rewrite方法,其实只要你能自定义错误页面(404),就完全可以用下面的方法实现高级Url优化。

1.设置404错误页面为404.php
2.在404.php在写入以下代码(第二行需更改为blog目录,如在/blog/就填写$blogdir=’blog/’,在根目录无需更改)
$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(”

HTTP/1.1 404 Not Found

 

“);
}

 

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



Comments are closed.