Nginx的Rewrite
经过网上查阅和测试,发现Nginx的Rewrite规则和Apache的Rewite规则差别不是很大,几乎可以直接使用。比如在Apache中这样写规则
rewrite ^/([0-9]{5}).html$ /viewthread.php?tid=$1 last;
而在Nginx中写成这样写是无法启动的,解决的办法是加上两个双引号:
rewrite “^/([0-9]{5}).html$” /viewthread.php?tid=$1 last;
同时将RewriteRule为Rewrite,基本就实现了Nginx的Rewrite规则到Apache的Rewite规则的转换。
Rewrite的Flags
last - 基本上都用这个Flag。
break - 中止Rewirte,不在继续匹配
redirect - 返回临时重定向的HTTP状态302
permanent - 返回永久重定向的HTTP状态301
官方文档请点击这里,另外如果对于302,301这些状态有疑问的,可以参考《301 Redirect 永久重定向的实现》:
http://www.ccvita.com/85.html
如果需要对Nginx配置防盗链的话,可以参考《Nginx的防盗链配置》:
http://www.ccvita.com/312.html
Discuz在Nginx下的Rewrite
rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last;
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;
rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;
rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
break;
SupeSite在Nginx下的Rewrite
rewrite ^([0-9]+)/spacelist(.*)$ index.php?$1/action_spacelist$2;
rewrite ^([0-9]+)/viewspace_(.+)$ index.php?$1/action_viewspace_itemid_$2;
rewrite ^([0-9]+)/viewbbs_(.+)$ index.php?$1/action_viewbbs_tid_$2;
rewrite ^([0-9]+)/(.*)$ index.php?$1/$2;
rewrite ^([0-9]+)$ index.php?$1;
rewrite ^action_(.+)$ index.php?action_$1;
rewrite ^category_(.+)$ index.php?action_category_catid_$1;
rewrite ^itemlist_(.+)$ index.php?action_itemlist_catid_$1;
rewrite ^viewnews_(.+)$ index.php?action_viewnews_itemid_$1;
rewrite ^viewthread_(.+)$ index.php?action_viewthread_tid_$1;
rewrite ^index([\.a-zA-Z0-9]*)$ index.php;
rewrite ^html/([0-9]+)/viewnews_itemid_([0-9]+)\.html$ index.php?action_viewnews_itemid_$2;
rewrite ^/([0-9]+)/spacelist(.+)$ /index.php?uid/$1/action/spacelist/type$2;
rewrite ^/([0-9]+)/viewspace(.+)$ /index.php?uid/$1/action/viewspace/itemid$2;
rewrite ^/([0-9]+)/viewbbs(.+)$ /index.php?uid/$1/action/viewbbs/tid$2;
rewrite ^/([0-9]+)/(.*)$ /index.php?uid/$1/$2;
rewrite ^/([0-9]+)$ /index.php?uid/$1;
rewrite ^/action(.+)$ /index.php?action$1;
rewrite ^/category(.+)$ /index.php?action/category/catid$1;
rewrite ^/viewnews(.+)$ /index.php?action/viewnews/itemid$1;
rewrite ^/viewthread(.+)$ /index.php?action/viewthread/tid$1;
rewrite ^/mygroup(.+)$ /index.php?action/mygroup/gid$1;
rewrite ^/bbs/(.*)
http://bbs.yaoyedan.com/$1;