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 ^ http://sitedomain.co.uk/new-name/unique-page-name/? permanent;

But this redirects to:



Direct quote from Pitfalls and Common Mistakes: Taxing Rewrites:

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;
  • 1
    It's definitely more readable. Will this also retain any queries that were on the original URL? – SteveEdson Oct 25 '13 at 9:56
  • 1
    Nope. It won't. If you really want to retain any queries upon permanent redirects, see this little trick... gist.github.com/pothi/7152399 – Pothi Kalimuthu Oct 25 '13 at 10:06
  • 1
    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
  • 1
    Using if along with return directive is perfectly fine. See wiki.nginx.org/IfIsEvil . Yes, IMO, it's better than using rewrite. – Pothi Kalimuthu 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;
  • 1
    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
  • 1
    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
  • 1
    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
  • 1
    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)(.*)$   http://sitedomain.co.uk/new-name/unique-page-name/$2 permanent;

Works a treat.


For me it worked without the equals sign like this:

location /old-url {
  return 301 /new-url;
  • 2
    You don't need the location block at all. Just use rewrite. See @SteveEdison's answer. – PKHunter Feb 26 '17 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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