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.

Lets say I have a website named xyz.co, I also have other domain names with the same prefix like xyz.com, xyz.it, xyz.co.it

Right now nginx works fine with server_name xyz.co in nginx.conf in port 80 I would want all the other domains to redirect to xyz.co also I would want www.* versions of the above to redirect to xyz.co. How can I get this? Is this nginx webserver level changes? or I need to make this changes in DNS?

share|improve this question
1  
Server names –  VBart Nov 29 '12 at 23:59

3 Answers 3

up vote 0 down vote accepted
server {
        listen 80;
        server_name  www.xyz.co xyz.com xyz.it xyz.co.it www.xyz.com www.xyz.it www.xyz.co.it;
        rewrite   ^  http://xyz.co$request_uri? permanent;
}

server {
        listen 80;
        server_name  xyz.co;

        ....................
        ....................
        ....................
}

You can remove the permanent flag from rewrite directive if you want a 302 redirect instead of a 301.

share|improve this answer
server {
    server_name ~^(?:www\.)?xyz\.(?:com|(?:co\.)?it)$;
    return http://xyz.co$request_uri;
}

or more effective:

server {
    listen 80;
    server_name xyz.com www.xyz.com
                xyz.it www.xyz.it
                xyz.co.it www.xyz.co.it;

    return http://xyz.co$request_uri;
}
share|improve this answer
    
although your answer was immensely helpful, since I already had a server block with lots of directives below, I was not sure what a return statement would do. @Ellimist answer clarified that. –  Srikar Appal Dec 3 '12 at 3:29

Documentation http://wiki.nginx.org/HttpRewriteModule

if ($http_host ~* "(www\.)?xyz\.(com|(co\.)?it)"){
  rewrite ^(.*)$ http://xyz.co/$1 break;
}

This will need testing and modifying to taste, but some quick tests show it should do what you need.

UPDATE per reference provided by VBart this: http://nginx.org/en/docs/http/converting_rewrite_rules.html is a much better method, for achieving what you require, see his answer provided.

share|improve this answer
    
This is a wrong, cumbersome, and ineffective way. (c) nginx.org/en/docs/http/converting_rewrite_rules.html –  VBart Nov 29 '12 at 23:58
    
cumbersome and ineffective I can agree with given the provided link showing a much better way to achieve this, wrong is debatable as the method works; in any event answer updated. –  Oneiroi Nov 30 '12 at 8:27
    
upvoting to negate the downvotes. This method might be wrong but it pointed me in right direction... –  Srikar Appal Dec 3 '12 at 3:27

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.