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

Being a bit of a newbie to NginX redirects and not particularly ninja with my regex, could someone please provide some points for how to do the following 301 redirects in a sane way.

(apologies for awful example names)

website.com/what -> website.com/en/what 
website.com/team -> website.com/en/team
website.com/stuff -> website.com/en/newstuff
website.com/stuff/this -> website.com/en/newstuff/foo/bar
website.com/stuff/that -> website.com/en/newstuff/thing/page

While i would love for someone to give me the exact answer - I'll settle for some good pointers or examples of roughly what i need to be doing or the sort of direction i should take.

If it helps I'm running nginx 0.8x on Ubuntu 11 and site itself is written in CodeIgniter,

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Cleanest way is to use a map block.

map $uri $new {    
  /aa                   /en/aa;
  /bb                   /newstuff/bb;
  ^/cc/(?P<suffix>.*)$  /$suffix;
  /john                 /users/john/;
}

server {
  server_name   www.example.com;
  rewrite  ^    $new   permanent;
}
share|improve this answer
1  
regex patterns like the third entry need to be prefixed with ~ –  kolbyjack Sep 7 '11 at 12:29
1  
@kolbyjack: That's totally the anti copy-paste measurement I include in all my posts. Yes, that's totally it. –  Martin Fjordvald Sep 7 '11 at 12:59
    
Thanks for pointing out maps, hadn't come across them before. –  James Butler Sep 7 '11 at 14:07

This should be the basic idea:

server {
  listen 80;
  server_name website.com;
  root /var/www/website/current/public;

  location /what {
    rewrite /en/what/ redirect;
  }

  location /team {
    rewrite /en/team/ redirect;
  }
}
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.