重定向后的结果是:http://example.com/new?para=xxx
如果改写成:rewrite ^/test.php(.*) /new? permanent;
那结果就是:http://example.com/new
所以,关键点就在于“?”这个尾缀。假如又想保留某个特定的参数,那又该如何呢?可以利用Nginx本身就带有的$arg_PARAMETER参数来实现。
例如:
把http://example.com/test.php?para=xxx&p=xx 重写向到 http://example.com/new?p=xx
可以写成:rewrite ^/test.php /new?p=$arg_p? permanent;
只求结果的朋友可以直接忽略前面的内容,看这里:
rewrite ^/test.php /new permanent; //重写向带参数的地址 rewrite ^/test.php /new? permanent; //重定向后不带参数 rewrite ^/test.php /new?id=$arg_id? permanent; //重定向后带指定的参数
permanent是永久重定向参数,根据需要去掉也可以,不过最好是带有。
首先Apache的Rewite规则差别不是很大,但是Nginx的Rewrite规则比Apache的简单灵活多了
Nginx可以用if进行条件匹配,语法规则类似C
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
Rewrite的Flags
last - 完成重写指令后,搜索相应的URI和位置。相当于Apache里的[L]标记,表示完成rewrite,不再匹配后面的规则。
break - 中止Rewirte,不在继续匹配。
redirect - 返回临时重定向的HTTP状态302。
permanent - 返回永久重定向的HTTP状态301。
ZEND Framework的重定向规则:
案例一:
全部重定向到 /index.php
rewrite ^/(.*) /index.php?$1&;
案例二:
如果文件或目录不存在则重定向到index.php
if (!-e $request_filename) {
rewrite ^/(.*) /index.php?$1&;
}
WordPress的重定向规则:
案例一:
http://www.wemvc.com/12 重定向到 http://www.wemvc.com/index.php?p=12
if (!-e $request_filename) {
rewrite ^/(.+)$ /index.php?p=$1 last;
}
案例二:
与zendframework配置很像
if (!-e $request_filename) {
rewrite ^/(.*) /index.php?$1&;
}
以下为Discuz完整的Rewrite for Nginx规则
if (!-f $request_filename) {
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=$3&page=$2 last;
rewrite ^/space-(username|uid)-(.+).html$ /space.php?$1=$2 last;rewrite ^/tag-(.+).html$ /tag.php?name=$1 last;








