[文章作者:张宴 本文版本:v1.1 最后修改:2009.12.01 转载请注明原文链接:http://blog.zyan.cc/bo-blog_nginx_rewrite/]
Bo-Blog是一款采用PHP开发的单用户博客程序,本人的博客也采用的是Bo-Blog,个人觉得bo-blog的排版、易用性要比WordPress好得多,但扩展性不如WordPress。
很多朋友向我询问过,Bo-Blog的Nginx Rewrite规则如何写。由于Bo-Blog官网只提供了Apache的Rewrite规则,这里,我将自己从 Bo-Blog 的 Apache Rewrite 规则转换而来的 Bo-Blog 2.1.1 的 Nginx Rewrite 重写规则贴在此处,供需要的朋友使用:
PS:2009-12-01修正一处错误,之前文章中的if (!-x更换为if (!-e
Bo-Blog是一款采用PHP开发的单用户博客程序,本人的博客也采用的是Bo-Blog,个人觉得bo-blog的排版、易用性要比WordPress好得多,但扩展性不如WordPress。
很多朋友向我询问过,Bo-Blog的Nginx Rewrite规则如何写。由于Bo-Blog官网只提供了Apache的Rewrite规则,这里,我将自己从 Bo-Blog 的 Apache Rewrite 规则转换而来的 Bo-Blog 2.1.1 的 Nginx Rewrite 重写规则贴在此处,供需要的朋友使用:
引用
if (!-e $request_filename)
{
rewrite ^/post/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?entryid=$1&page=$2&part=$3 last;
rewrite ^/page/([0-9]+)/([0-9]+)/?$ /index.php?mode=$1&page=$2 last;
rewrite ^/starred/([0-9]+)/?([0-9]+)?/?$ /star.php?mode=$1&page=$2 last;
rewrite ^/category/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=category_$1&mode=$2&page=$3 last;
rewrite ^/archiver/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4 last;
rewrite ^/date/([0-9]+)/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=showday_$1-$2-$3&mode=$4&page=$5 last;
rewrite ^/user/([0-9]+)/?$ /view.php?go=user_$1 last;
rewrite ^/tags/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /tag.php?tag=$1&mode=$2&page=$3 last;
rewrite ^/component/id/([0-9]+)/?$ /page.php?pageid=$1 last;
rewrite ^/component/([^/]+)/?$ /page.php?pagealias=$1 last;
#Force redirection for old rules
rewrite ^/read\.php/([0-9]+)\.htm$ http://$host/post/$1/ permanent;
rewrite ^/post/([0-9]+)\.htm$ http://$host/post/$1/ permanent;
rewrite ^/post/([0-9]+)\_([0-9]+)\.htm$ http://$host/post/$1/$2/ permanent;
rewrite ^/post/([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/post/$1/$2/$3/ permanent;
rewrite ^/index\_([0-9]+)\_([0-9]+)\.htm$ http://$host/page/$1/$2/ permanent;
rewrite ^/star\_([0-9]+)\_([0-9]+)\.htm$ http://$host/starred/$1/$2/ permanent;
rewrite ^/category\_([0-9]+)\.htm$ http://$host/category/$1/ permanent;
rewrite ^/category\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/category/$1/$2/$3/ permanent;
rewrite ^/archive\_([0-9]+)\_([0-9]+)\.htm$ http://$host/archiver/$1/$2/ permanent;
rewrite ^/archive\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/archiver/$1/$2/$3/$4/ permanent;
rewrite ^/showday\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/date/$1/$2/$3/ permanent;
rewrite ^/showday\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/date/$1/$2/$3/$4/$5/ permanent;
#Filename alias
rewrite ^/([a-zA-Z0-9_-]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?blogalias=$1&page=$2&part=$3 last;
}
{
rewrite ^/post/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?entryid=$1&page=$2&part=$3 last;
rewrite ^/page/([0-9]+)/([0-9]+)/?$ /index.php?mode=$1&page=$2 last;
rewrite ^/starred/([0-9]+)/?([0-9]+)?/?$ /star.php?mode=$1&page=$2 last;
rewrite ^/category/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=category_$1&mode=$2&page=$3 last;
rewrite ^/archiver/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4 last;
rewrite ^/date/([0-9]+)/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=showday_$1-$2-$3&mode=$4&page=$5 last;
rewrite ^/user/([0-9]+)/?$ /view.php?go=user_$1 last;
rewrite ^/tags/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /tag.php?tag=$1&mode=$2&page=$3 last;
rewrite ^/component/id/([0-9]+)/?$ /page.php?pageid=$1 last;
rewrite ^/component/([^/]+)/?$ /page.php?pagealias=$1 last;
#Force redirection for old rules
rewrite ^/read\.php/([0-9]+)\.htm$ http://$host/post/$1/ permanent;
rewrite ^/post/([0-9]+)\.htm$ http://$host/post/$1/ permanent;
rewrite ^/post/([0-9]+)\_([0-9]+)\.htm$ http://$host/post/$1/$2/ permanent;
rewrite ^/post/([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/post/$1/$2/$3/ permanent;
rewrite ^/index\_([0-9]+)\_([0-9]+)\.htm$ http://$host/page/$1/$2/ permanent;
rewrite ^/star\_([0-9]+)\_([0-9]+)\.htm$ http://$host/starred/$1/$2/ permanent;
rewrite ^/category\_([0-9]+)\.htm$ http://$host/category/$1/ permanent;
rewrite ^/category\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/category/$1/$2/$3/ permanent;
rewrite ^/archive\_([0-9]+)\_([0-9]+)\.htm$ http://$host/archiver/$1/$2/ permanent;
rewrite ^/archive\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/archiver/$1/$2/$3/$4/ permanent;
rewrite ^/showday\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/date/$1/$2/$3/ permanent;
rewrite ^/showday\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\_([0-9]+)\.htm$ http://$host/date/$1/$2/$3/$4/$5/ permanent;
#Filename alias
rewrite ^/([a-zA-Z0-9_-]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?blogalias=$1&page=$2&part=$3 last;
}
PS:2009-12-01修正一处错误,之前文章中的if (!-x更换为if (!-e
/articles/PHP-safe/ (自定义别名的)
articles/131/
在nginx下用了你这个规则,我把其中的post全部替换为articles了,大部分可以访问,但是有二级目录的文章自定义别名的是404,另外还有tags里面的中文也是404,不知如果换成我以前的articles方式,规则还要怎样修改才能完全正确访问呢
编辑inc/mod_visit.php 文件
找到 else { //Deal with ajax
然后再下面添加:
$site="http://book.shafa.in";
///make html
$postida=str_replace("'","",$plusquery_c);
$postida=str_replace(",","",$postida);
$deldira="post/".$postida;
$deldirs="post/";
if ($deldira != $deldirs) //is post root
{
if(is_dir($deldira)) removeDir($deldira);
$cur_chapcont=file_get_contents($site."/post/".$postida);
if(!is_dir($deldira)) mkdir($deldira);
$fp=fopen("post/".$postida."/index.html","w+");
fwrite($fp,$cur_chapcont);
if ($fp) fclose($fp);
}
///end make html
在文件末尾添加:
//删除目录
function removeDir($dirName)
{
$result = false;
if(! is_dir($dirName))
{
trigger_error("目录名称错误", E_USER_ERROR);
}
$handle = opendir($dirName);
while(($file = readdir($handle)) !== false)
{
if($file != '.' && $file != '..')
{
$dir = $dirName . DIRECTORY_SEPARATOR . $file;
is_dir($dir) ? removeDir($dir) : unlink($dir);
}
}
closedir($handle);
$result = rmdir($dirName) ? true : false;
return $result;
}
<VirtualHost *:80>
ServerName netjiajia.com
RewriteLogLevel 3
DocumentRoot /home/tom/code/netjiajia
DirectoryIndex index.jsp
JkMount /*.jsp ajp13
JkMount /*.do ajp13
JkMount /*.html ajp13
JkMount /image* ajp13
JkMount /dwr* ajp13
JkMount /uploadfile* ajp13
JkMount /home ajp13
JkMount /swf* ajp13
RewriteEngine on
RewriteCond %{HTTP_HOST} (?!www|bbs|home|admin|cms|image)^[a-z0-9\-]+\.netjiajia\.com$
RewriteRule ^/(.*)$ /%{HTTP_HOST}/$1
RewriteRule ^/gonglue\.netjiajia\.com/$ /gonglue.html [PT]
RewriteRule ^/shopping\.netjiajia\.com/$ /shopping.html [PT]
RewriteRule ^/food\.netjiajia\.com/$ /food.html [PT]
RewriteRule ^/journey\.netjiajia\.com/$ /journey.html [PT]
RewriteRule ^/fashion\.netjiajia\.com/$ /fashion.html [PT]
RewriteRule ^/shenzhen\.netjiajia\.com/$ /city/shenzhen.jsp [PT]
RewriteRule ^/guangzhou\.netjiajia\.com/$ /city/guangzhou.jsp [PT]
RewriteRule ^/hangzhou\.netjiajia\.com/$ /city/hangzhou.jsp [PT]
RewriteRule ^/nanjing\.netjiajia\.com/$ /city/nanjing.jsp [PT]
RewriteRule ^/zhanjiang\.netjiajia\.com/$ /city/zhanjiang.jsp [PT]
RewriteRule ^/beijing\.netjiajia\.com/$ /city/beijing.jsp [PT]
RewriteRule ^/xiamen\.netjiajia\.com/$ /city/xiamen.jsp [PT]
RewriteRule ^/chongqing\.netjiajia\.com/$ /city/chongqing.jsp [PT]
RewriteRule ^/huizhou\.netjiajia\.com/$ /city/huizhou.jsp [PT]
RewriteRule ^/suzhou\.netjiajia\.com/$ /city/suzhou.jsp [PT]
RewriteRule ^/nanchang\.netjiajia\.com/$ /city/nanchang.jsp [PT]
RewriteRule ^/shanghai\.netjiajia\.com/$ /city/shanghai.jsp [PT]
RewriteRule ^/wuhan\.netjiajia\.com/$ /city/wuhan.jsp [PT]
RewriteRule ^/haikou\.netjiajia\.com/$ /city/haikou.jsp [PT]
RewriteRule ^/xian\.netjiajia\.com/$ /city/xian.jsp [PT]
RewriteRule ^/elements\.netjiajia\.com/$ /elements.jsp [PT]
RewriteRule ^/forum\.netjiajia\.com/$ /club.html [PT]
RewriteRule ^/daigou\.netjiajia\.com/$ /purchase/list.html [PT]
RewriteRule ^/accakappa\.netjiajia\.com/$ /acca_kappa4.jsp [PT]
RewriteRule ^/vip\.netjiajia\.com/$ /sale/hycard.jsp [PT]
RewriteRule ^/ayi\.netjiajia\.com/$ /shop/ayi.html [PT]
RewriteRule ^/retail\.netjiajia\.com/$ /retail.jsp [PT]
RewriteRule ^/brandshop\.netjiajia\.com/$ /shop/brandshop.html [PT]
RewriteRule ^/([a-z0-9\-]+)\.netjiajia\.com/(.*)$ /$2 [PT]
#RewriteEngine on
#RewriteCond %{HTTP_REFERER} !^http://netjiajia.com[/size]/.*$ [NC]
#RewriteCond %{HTTP_REFERER} !^http://netjiajia.com$ [NC]
#RewriteCond %{HTTP_REFERER} !^http://www.netjiajia.com/.*$ [NC]
#RewriteCond %{HTTP_REFERER} !^http://www.chinhkyoula.com$ [NC]
#RewriteRule .*.(gif|jpb|png|css|js|swf|jgp|jpeg|bmp])$ http://www.chkyoula.com [R,NC] 銆€銆€
#SetEnvIfNoCase Referer "^http://netjiajia.com" local_ref=1
#SetEnvIfNoCase Referer "^http://www.netjiajia.com" local_ref=1
#<FilesMatch ".(gif|jpb|png|css|js|swf|jpg|jpeg|bmp)">
#Order Allow,Deny
#Allow from env=local_ref
#</FilesMatch>
LoadModule jk_module modules/mod_jk.so
RewriteLog "/usr/local/apache2/logs/rewrite.log"
JkLogFile "/usr/local/tomcat/logs/mod_jk2.log"
JkLogLevel info
</VirtualHost>
ecshop的文章重写规则如下
rewrite "^/article-([0-9]+)(.*)\.html$" /article.php?id=$1 last;
我想把其中id=1 2 4 5的页面分别重写成disclaimer.html,这样的自定义名字,下面这样可以吗?
rewrite "^disclaimer\.html" /article.php?id=1 permanent ;
rewrite "^copyright\.html" /article.php?id=2 permanent ;
rewrite "^contactus\.html" /article.php?id=4 permanent ;
rewrite "^aboutus\.html" /article.php?id=5 permanent ;