I have a staging site that I test anything relating to the website
before we deploy. I have an nginx config that I also test with for this
staging site.
I wanted to rewrite a uri segment where a section of the site changed
names.
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.
That’s exactly what I thought, but when I used (.*) at the end and used
the $1 I kept getting 404. When I made it the way it is now, it worked
on my staging site as expected but not on my live site.
I can confirm that on neither site, the following does not work (404
error):
location ‘/old’ only catches ‘/old’, not even ‘/old/’ and of course
nothing
lie ‘/old/…’
Then, inside, you rewrite only URI which start with ‘/old/’, so in
fine,
nothing will be ever redirected.
The machine does precisely what you asked it to do.
will not cause a redirection for any request that starts /old/, when
replacing that location with
location /old {
rewrite ^/old? /new permanent;
}
will lead to the expected redirection.
The solution you found was to change the location definition to avoid
trying any regex locations. That suggests that you had other regex
locations which were being matched instead of the ones above. But
independent of that, the above two locations should redirect almost all
of the same requests (all bar exactly “/old”) – so if the rest of the
configuration didn’t change in between the two tests, you have found
something odd.
here is the current config, with prior rewrites before this location
block:
For information: because one request is handled by one location, the
order of location blocks within the config file is not relevant (apart
from regex ones).
Which just means that “the most recent match” may not be from the
location
block a few lines earlier in the config file.