Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm sure this has been asked before, but I can't find a solution that works.

A website has switched CMS services, but has the same domain, how do I set up an nginx rewrite for a single page?


Old Page

New page

Please note, I don't want everything within the content page to be redirected, but literally just the url mentioned above. I have about 9 redirects to set up, non of which fit in a pattern.


Edit: I found this solution, which seems to be working, except for the fact that it redirects without a slash:

if ( $request_filename ~ content/unique-page-name/ ) {
   rewrite ^ permanent;

But this redirects to:

share|improve this question
up vote 37 down vote accepted

Direct quote from

By using the return directive we can completely avoid evaluation of regular expression.

Please use return instead of rewrite for permanent redirects. Here's my approach to this use-case...

location = /content/unique-page-name {
  return 301 /new-name/unique-page-name;
share|improve this answer
It's definitely more readable. Will this also retain any queries that were on the original URL? – SteveEdson Oct 25 '13 at 9:56
Nope. It won't. If you really want to retain any queries upon permanent redirects, see this little trick... – Pothi Oct 25 '13 at 10:06
Am I right in thinking that using if statements in nginx is bad? Would this still be better practice than using the rewrite method? – SteveEdson Oct 25 '13 at 10:10
Using if along with return directive is perfectly fine. See . Yes, IMO, it's better than using rewrite. – Pothi Oct 25 '13 at 10:22

Ideally you shouldn't use if statements if you can avoid it. Something like this could work (untested).

location ~ /content/(.*)$ {
    rewrite ^ /new-name/$1?$args permanent;
share|improve this answer
Wouldn't that catch everything within the content directory? I only want to redirect the individual item. For example, I have another item in the content directory, but I need it redirecting to .../another-new-name/unique-page-name2 – SteveEdson Oct 25 '13 at 9:20
I've solved it. Turns out it redirecting correctly, but the script is was hitting was mangling the URL after. – SteveEdson Oct 25 '13 at 9:31
Ah, I misunderstood the question. I thought you wanted only the content part to change. The principle remains the same. I'm glad its working for you. – Gevious Oct 25 '13 at 9:37
No problem, thanks for the help though, I'll definitely need to use that in the future. – SteveEdson Oct 25 '13 at 9:54

I used the following solution:

rewrite ^(/content/unique-page-name)(.*)$$2 permanent;

Works a treat.

share|improve this answer

For me it worked without the equals sign like this:

location /old-url {
  return 301 /new-url;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.