Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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) -> -> -> -> ->

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
up vote 5 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 {
  rewrite  ^    $new   permanent;
share|improve this answer
regex patterns like the third entry need to be prefixed with ~ – kolbyjack Sep 7 '11 at 12:29
@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;
  root /var/www/website/current/public;

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

  location /team {
    rewrite /en/team/ redirect;
share|improve this answer

Your Answer


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.