Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

}

=========

share|improve this question

2 Answers 2

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

share|improve this answer

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