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.

For some reason www.server-1.com/forum doesn't redirect to forum.server-1.com but www.server-2.com. When I delete the part where it redirects www.server-1.com to www.server-2.com it works like a charm... well, not quite, since the first slash after forum get's somehow lost in the process whenever the URL doesn't end with /forum but, for example, with /forum/index.php.

# Redirect www.server-1.com/forum to forum.server-1.com
# Redirect www.server-1.com/forum/ to forum.server-1.com
# Redirect www.server-1.com/forums to forum.server-1.com
# Redirect www.server-1.com/forums/ to forum.server-1.com
# Redirect www.server-1.com to www.server-2.com
server {
  listen 80;

  server_name www.server-1.com;

  location = /forum {
    rewrite ^ http://forum.server-1.com redirect;
    break;
  }

  location /forum/ {
    rewrite ^/forum/(.*) http://forum.server-1.com$1 redirect;
    break;
  }

  location = /forums {
    rewrite ^ http://forum.server-1.com redirect;
    break;
  }

  location /forums/ {
    rewrite ^/forums/(.*) http://forum.server-1.com$1 redirect;
    break;
  }

  rewrite ^ http://www.server-2.com redirect;
}

# Redirect forum.server-1.com to www.server-3.com/forum (where the forum is hosted)
server {
  listen 80;

  server_name forum.server-1.com;

  rewrite ^(.*) http://www.server-3.com/forum$1 redirect;
}

# Redirect forums.server-1.com to forum.server-1.com
server {
  listen 80;

  server_name forums.server-1.com;

  rewrite ^(.*) http://forum.server-1.com$1 redirect;
}

# Redirect server-1.com and *server-1.com to www.server-1.com
server {
  listen 80;

  server_name server-1.com *.server-1.com;

  rewrite ^(.*) http://www.server-1.com$1 redirect;
}
share|improve this question

1 Answer 1

nginx doesn't necessarily process directives in the order in which they appear in the config file. server-level rewrite directives will be processed before location selection, which explains the behavior you're seeing. Since everything in the server is just a rewrite, you should be able to use this:

server {
  server_name www.server-1.com;

  rewrite ^/forums?(.*) http://forum.server-1.com$1 redirect;
  return 302 http://www.server-2.com/;
}
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.