如果查询参数存在于nginx上,则重定向

提问

我正在使用IPB论坛.我设法使用友好的网址与nginx服务器配置修改.但是,我需要将旧论坛的URL重定向到重定向器php文件,以获取主题(或论坛,成员等)的当前URL.例如:如果url类似于/forum/index.php?board=23,我将重定向到redirector.php.

这是我目前的配置,能够在IPB上使用友好的URL

    location /forum {
        try_files $uri $uri/ /forum/index.php;
        rewrite ^ /forum/index.php? last;
    }

当我在这个位置块中插入if语句时,如下所示,我无法检索查询参数“board”.

location /forum {
        if ($arg_board != "") {
            rewrite ^ /redirector.php?q=$arg_board break;
        }
        try_files $uri $uri/ /forum/index.php;
        rewrite ^ /forum/index.php? last;
    }

这里缺少什么?

最佳答案

你的问题与使用break而不是last.从文档:

http://wiki.nginx.org/HttpRewriteModule#rewrite

last – completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.

break – completes processing of current rewrite directives and non-rewrite processing continues within the current location block only.

由于您没有在/ forum位置块中为/ redirector定义处理程序,因此if(..){rewrite}不会执行您想要的操作.将该断点设为最后一个,以便重写可以触发相应的位置块.