Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirection if query parameter exists on nginx

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?

like image 695
emregecer Avatar asked May 21 '12 21:05

emregecer


People also ask

Can Nginx location blocks match a URL query string?

Can nginx location blocks match a URL query string? Short answer: No.

What is Nginx permanent redirect?

Permanent redirects such as NGINX 301 Redirect simply makes the browser forget the old address entirely and prevents it from attempting to access that address anymore. These redirects are very useful if your content has been permanently moved to a new location, like when you change domain names or servers.


1 Answers

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.

like image 155
Matthew Franglen Avatar answered Sep 19 '22 23:09

Matthew Franglen