rewrite problem

Maxim Dounin mdounin at mdounin.ru
Tue Sep 4 07:58:29 UTC 2012


Hello!

On Tue, Sep 04, 2012 at 10:59:40AM +0800, sosogh wrote:

> HI list
> I want to redirect
> http://www.mysite.com/bbs/viewthread.php?tid=8123&extra=
> to
> http://www.mysite.com/forum.php?mod=viewthread&tid=8123
>  
> This is what I  place in the "server" block in the nginx.conf
> rewrite  ^/bbs/viewthread.php?tid=([0-9]+).*$   /forum.php?mod=viewthread&tid=$1 last;
>  
> but when visit http://www.mysite.com/bbs/viewthread.php?tid=8123&extra=.
> It shows 404 error.
>  
> Could anyone point me out what is wrong?
> Thank you!

The rewrite in question won't match anything as rewrite work with 
URI without arguments.  Use something like this instead:

location = /bbs/viewthread.php {
    if ($arg_tid ~ ^[0-9]+$) {
        rewrite ^ /forum.php?mod=viewthread&tid=$arg_tid? last;
    }

    ...
}

Some more details may be found here:

http://nginx.org/r/location
http://nginx.org/r/rewrite
http://nginx.org/r/if

Maxim Dounin



More information about the nginx mailing list