1

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?

3 Answers3

3
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;
}
VBart
  • 8,309
  • 3
  • 25
  • 26
  • 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 Appalaraju Dec 03 '12 at 03:29
0
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.

Ellimist
  • 129
  • 3
-1

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.

Oneiroi
  • 2,063
  • 1
  • 15
  • 28
  • 1
    **This is a wrong, cumbersome, and ineffective way.** (c) http://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 08:27
  • upvoting to negate the downvotes. This method might be wrong but it pointed me in right direction... – Srikar Appalaraju Dec 03 '12 at 03:27