Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGinx Domain name redirects

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?

UPDATE: I tried this in nginx.conf but no use...

server
{
listen 80;
server_name xyz.co xyz.com, xyz.it, xyz.co.it;
rewrite ^/(.*) http://xyz.co permanent;
}

I first tried posting this question in ServerFault but no response there - https://serverfault.com/questions/453472/nginx-domain-name-redirects

like image 900
Srikar Appalaraju Avatar asked Jul 08 '26 06:07

Srikar Appalaraju


1 Answers

add one server block for all the domain names that need to be redirected. like this:

server {
    listen 80;
    server_name xyz.com, xyz.it, xyz.co.it;
    rewrite ^ http://xyz.co$request_uri permanent;
}

and another server block for the xyz.co domain:

server {
    listen 80;
    server_name xyz.co;

    #other settings
}

this way when you go to one of the domain names that need to be redirected nginx will simply redirect to xyz.co and move into the other server block where you can add all your settings (rootfolder, location blocks, etc)

like image 136
Jap Mul Avatar answered Jul 09 '26 20:07

Jap Mul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!