Right now my nginx is rewriting several domains to one main domain which we are using. Here's one rule from my config:

server {
  listen X.X.X.X:80;
  server_name .exampleblog.org;
  rewrite ^(.*) http://blog.example.org$1 permanent;
}

Every request to *exampleblog.org is redirected to blog.example.org

Now I want www.exampleblog.org/+ and exampleblog.org/+ to redirect the user to our Google Plus page. It tried different versions of:

server {
  listen X.X.X.X:80;
  server_name .exampleblog.org;
  location /+ {
    rewrite ^ https://plus.google.com/12345678901234567890/ permanent;
  }
  rewrite ^(.*) http://blog.example.org$1 permanent;
}

Above and other versions just redirect to blog.example.org/+ - what am I doing wrong?

link|improve this question
feedback

1 Answer

up vote 3 down vote accepted

Directives in nginx don't necessarily apply in the order they appear in the configuration file. The server-level rewrite acts before a location is selected, and it always matches, so it will redirect everything. You need a second location like so:

server {
  listen x.x.x.x:80;
  server_name .exampleblog.org;

  # Match /+ requests exactly    
  location = /+ {
    # I prefer return 301 instead of rewrite ^ <target> permanent,
    # but you can use either
    return 301 http://plus.google.com/1234567890/;
  }

  # Match everything else
  location / {
    return 301 http://blog.example.org$request_uri;
  }
}
link|improve this answer
Aaah! Didn't try with a second location - thank you very much for the quick help :) Works perfectly! – lorem monkey Nov 16 '11 at 16:38
feedback

Your Answer

 
or
required, but never shown

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