1

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,

2 Answers 2

5

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;
}
3
  • 1
    regex patterns like the third entry need to be prefixed with ~
    – kolbyjack
    Sep 7, 2011 at 12:29
  • 1
    @kolbyjack: That's totally the anti copy-paste measurement I include in all my posts. Yes, that's totally it. Sep 7, 2011 at 12:59
  • Thanks for pointing out maps, hadn't come across them before. Sep 7, 2011 at 14:07
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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