Redirection if query parameter exists on nginx

15,007

Your problem relates to the use of break instead of last. From the documentation:

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.

Since you do not define a handler for the /redirector within the /forum location block, your if(..) { rewrite } does not do what you want. Make that break a last, so that the rewrite can trigger the appropriate location block.

Share:
15,007
emregecer
Author by

emregecer

Updated on June 16, 2022

Comments

  • emregecer
    emregecer almost 2 years

    I'm using IPB forums. I managed to use friendly urls with nginx server conf modifications. However I need to redirect my old forum's URLs to a redirector php file to get current url of a topic (or forum, member etc.). For example: if url is like /forum/index.php?board=23, I will do a redirection to redirector.php .

    This is my current configuration to be able to use friendly URLs on IPB

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

    When I do insert an if statement inside this location block like the following, I can not retrieve query parameter "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;
        }
    

    What is missing in here?