In Nginx, I would like to redirect my subdomain.domain.com to domain.com/sub/ .how do I do that ? I would like to use the existing sites config file for domain.com instead of creating a new one.

the config file I have is (how do I add to this)

======

server {
listen   80;
server_name www.domain.com domain.com  *.domain.com;
access_log /srv/www/domain.com/logs/access.log;
error_log /srv/www/domain.com/logs/error.log;

location / {
    root   /srv/www/domain.com/public_html;
    index  index.html index.htm;
}

location /phpmyadmin { root /usr/share; index index.php; }

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  /srv/www/domain.com/public_html$fastcgi_script_name;
}

}

=========

link|improve this question
feedback

2 Answers

Sorry Mike, your if statement solution is a bad idea... refer to the Nginx pitfalls wiki page for why...

add another server block above the one you have containing this

server {
   server_name subdomain.domain.com
   rewrite ^ $scheme://domain.org/subdomain$request_uri redirect;
}

and magic will be done...

link|improve this answer
feedback

This would go in your server block. This is untested but should work or give you an idea

if ($host ~* "^(.*)\.domain\.com$"){
  set $subd $1;
  rewrite ^(.*)$ http://domain.com/$subd permanent;
  break;
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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