几个Nginx子目录rewrite的例子

王朝学院·作者佚名  2011-04-10
窄屏简体版  字體: 小  |  中  |  大  |  超大  

我们已经有很多客户在 VPS 上使用 Nginx,对于刚从 Apache 转过来的客户最常遇到的一个问题就是怎么弄 Nginx 下的 rewrite 以及怎么把 Apache 里的 .htaccess 转化成 Nginx,网上关于这方面的资料一大堆,关于 WordPRess, discuz, phpcms, ecshop, shopex 等的 rewrite 应有尽有,直接 copy 就可以。还有一个 Nginx 新手常见的问题是拿到这些 rewrite 规则后不知道怎么改,比如 Nginx 下子目录的 rewrite 应该改成什么样子?/ 下是 wordpress,/bbs 下装个 discuz,/ 是 discuz,/blog 下装个 wordpress 或者 / 下是 wordpress,/blog 下再装个 wordpress 等,这样的 rewrite 怎么改呢?弄几个例子放到我们的 FAQ 里供参考:

WordPress 安装在子目录 /blog 下:

location /blog/ {

root /home/www/vpsee.com;

index index.php index.html index.htm;

if (!-e $request_filename) {

rewrite ^.+/?(/blog/wp-.*) $1 last;

rewrite ^.+/?(/blog/.*\.php)$ $1 last;

rewrite ^(.+)$ /blog/index.php?q=$1 last;

}

}

Discuz! 7.2 安装在子目录 /bbs 下:

location /bbs/ {

root /home/www/vpsee.com;

index index.php index.html index.htm;

rewrite ^/bbs/archiver/((fid|tid)-[\w\-]+\.html)$ /bbs/archiver/index.php?$1 last;

rewrite ^/bbs/forum-([0-9]+)-([0-9]+)\.html$ /bbs/forumdisplay.php?fid=$1&page=$2 last;

rewrite ^/bbs/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /bbs/viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;

rewrite ^/bbs/space-(username|uid)-(.+)\.html$ /bbs/space.php?$1=$2 last;

rewrite ^/bbs/tag-(.+)\.html$ /bbs/tag.php?name=$1 last;

}

Discuz! X1.5 安装在子目录 /bbs 下:

location /bbs/ {

root /home/www/vpsee.com;

index index.php index.html index.htm;

rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;

rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;

rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;

rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;

rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;

rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;

rewrite ^([^\.]*)/([a-z]+)-(.+)\.html$ $1/$2.php?rewrite=$3 last;

if (!-e $request_filename) {

return 404;

}

}

如果对理解 ^([^\.]*)/([a-z]+)-(.+)\.html$ 这样的正则表达式有困难并对这方面有兴趣的话可以看看一些书,最好的一本应该是 O’Reilly 出的 Mastering Regular Expressions(也有中文版:《精通正则表达式》)。

文章来源:www.vpsee.com

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航