Redirection issues

Hello everyone,

I’m currently using Nginx 1.4.1 on CentOS 6.4 64bits and trying to
perform a
redirect of this type:
location /alpha {
location ~ ^/alpha/script.+([\d]+)$ {
return 301 /beta/info/$1/;
}
}

The originating location is /alpha/script.php?id=328.
The redirected location is /beta/info/328/.

For some reason, the above redirect returns a 404 with the URL:
http://www.domain.com/beta/script.php
Which does not exist anymore. It looks like the URI is not parsed from
the
regex?
I also tried this format, without success:
location ~ ^/alpha/script.php?id=([\d]+)$ {

Thank you for your help.

Posted at Nginx Forum:

Hello!

On Mon, Jun 24, 2013 at 08:58:29AM -0400, TECK wrote:

The originating location is /alpha/script.php?id=328.
The redirected location is /beta/info/328/.

For some reason, the above redirect returns a 404 with the URL:
http://www.domain.com/beta/script.php
Which does not exist anymore. It looks like the URI is not parsed from the
regex?
I also tried this format, without success:
location ~ ^/alpha/script.php?id=([\d]+)$ {

Query string isn’t considered by location matching. If you want
to test request arguments, you have to do it inside a location
matched, like this:

location = /alpha/script.php {
    if ($arg_id) {
        return 301 /beta/info/$arg_id/;
    }

    ...
}

In this particular case test is likely useless, and just a return
will be enough.


Maxim D.
http://nginx.org/en/donation.html

Thank you Maxim. :slight_smile:
Is there a way to skip the IF with try_files and still having a 301
footprint?
Something like:
try_files /beta/info/$arg_id/ =404;

Posted at Nginx Forum: