rewrite url segment staging site and live site

Francis Daly francis at
Tue Jun 12 17:23:16 UTC 2012

On Tue, Jun 12, 2012 at 10:47:10AM -0400, caleboconnell wrote:

Hi there,

>   location /old {
>     rewrite ^/old/? /new/$1 permanent;
>   }

What do you think $1 is set to here?

(Usually, it is "the last thing matched". So usually, it is worth making
sure that you match something immediately before using it.)

> what I want:
>  -->
> what I get:
> -->

"regex" can include things inside parentheses, which are then available
as ${number} in "replacement".

   location /old/ {
     rewrite ^/old/(.*) /new/$1 permanent;

will probably do what you want; but read about the question mark, in
case it matters.

> It's fine for now, but I don't know why the exact config would work
> different.

Usually, the same config works the same way. If you've found a case where
that isn't the case, it may be worth investigating. But it is potentially
the *whole* config that matters here, where you use $1 without showing
where it is set.

Francis Daly        francis at

More information about the nginx mailing list