Nginx rewrites incorrectly

Posted by Oliver K. on Server Fault See other posts from Server Fault or by Oliver K.
Published on 2011-11-22T09:13:43Z Indexed on 2011/11/22 9:59 UTC
Read the original article Hit count: 637

Filed under:
|
|
|

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

Thanks in advance!

© Server Fault or respective owner

Related posts about nginx

Related posts about http