Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

E.g.

Old Page

http://sitedomain.co.uk/content/unique-page-name

New page

http://sitedomain.co.uk/new-name/unique-page-name

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.

Thanks!

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:

http://sitedomain.co.uknew-name/unique-page-name/

share|improve this question

3 Answers 3

Direct quote from http://wiki.nginx.org/Pitfalls#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;
}
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... gist.github.com/pothi/7152399 –  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 wiki.nginx.org/IfIsEvil . 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)(.*)$   http://sitedomain.co.uk/new-name/unique-page-name/$2 permanent;

Works a treat.

share|improve this answer

Your Answer

 
discard

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.