Nginx redirect one path to another

Solution 1:

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;
}

Solution 2:

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;
} 

Solution 3:

I used the following solution:

rewrite ^(/content/unique-page-name)(.*)$   http://sitedomain.co.uk/new-name/unique-page-name/$2 permanent;

Works a treat.


Solution 4:

For me it worked without the equals sign like this:

location /old-url {
  return 301 /new-url;
}

Tags:

Nginx

Rewrite