# Redirection issues

**URL:** <https://www.ruby-forum.com/t/redirection-issues/245631>\
**Category:** NGINX\
**Created:** [June 24, 2013, 2:59pm UTC](https://www.ruby-forum.com/t/redirection-issues/245631 "2013-06-24T14:59:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [June 24, 2013, 2:59pm UTC](https://www.ruby-forum.com/t/redirection-issues/245631/1 "2013-06-24T14:59:09Z")

</div>

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](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:

> **[Redirection issues](https://forum.nginx.org/read.php?2,240299,240299#msg-240299)**
>
> 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

---

<div class="post-metadata">

**Author:** ![teck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b2d939/32.png) [@teck](https://www.ruby-forum.com/u/teck)\
**Post date:** [June 24, 2013, 3:14pm UTC](https://www.ruby-forum.com/t/redirection-issues/245631/2 "2013-06-24T15:14:20Z")

</div>

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](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.

> **[nginx: donation](http://nginx.org/en/donation.html)**

---

<div class="post-metadata">

**Author:** ![teck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b2d939/32.png) [@teck](https://www.ruby-forum.com/u/teck)\
**Post date:** [June 24, 2013, 3:43pm UTC](https://www.ruby-forum.com/t/redirection-issues/245631/3 "2013-06-24T15:43:32Z")

</div>

Thank you Maxim. 🙂  
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:

> **[Re: Redirection issues](https://forum.nginx.org/read.php?2,240299,240302#msg-240302)**
>
> 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
