Redirect based on hostname

I’ve read the whole NginX wiki and couldn’t find a solution yet. So
would you be so gentle to tell me how to do this.

I have two (2) domains on the same website. Let’s say “www.first.com
and “www.second.com
The frontend php shares the same files and the only difference is in the
layout so I must differentiate the layout based on the hostname header
sent by client.

My goal would be to redirect to the www domain if an invalid subdomain
is typed.

example: error.first.com must redirect to www.first.com as well as
wrong.second.com must redirect to www.second.com

My setup when I was using a single domain was easy:

    if ($host !~* "www.first.com") {
        rewrite ^(.*)$ http://www.first.com$1 permanent;
        break;
        }

but now my need is to do something like:

    if ($host !~* "www.first.com|www.second.com") {
        rewrite ^(.*)$ (what to put here???)$1 permanent;
        break;
        }

Thanks to anyone able to help me out with this issue…

Posted at Nginx Forum:

On 23.03.2011, at 15:40, sevy wrote:

My goal would be to redirect to the www domain if an invalid subdomain
break;

Thanks to anyone able to help me out with this issue…

http://nginx.org/en/docs/http/converting_rewrite_rules.html


Igor S.
http://sysoev.ru/en/

Thanks Igor, you are always the best!
And it’s pretty obvious when speaking with the programmer of the best
http server of the planet :slight_smile:

Posted at Nginx Forum: