Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx rewrite only when root domain

Tags:

nginx

I'd like to do an Nginx rewrite where I have two domains: domain.com and domain.net with the following rules:

1) If a user goes to http://www.domain.net/, he will be redirected to http://www.domain.com/ 2) If a user goes to http://www.domain.net/anything_else.html the rewrite will not occur.

This is my failed attempt:

server {
  listen 80;
  server_name www.domain.net domain.net;
  location / {
    rewrite / http://www.domain.com/ permanent;
  }
}

The correct format would be much appreciated!

like image 820
Mauvis Ledford Avatar asked Feb 17 '12 22:02

Mauvis Ledford


2 Answers

Unfortunately, @blueberryfields answer didn't quite work for me, had to do it slightly different:

server {
  (..)
  location / {
    rewrite ^(/)$ http://www.domain.com/ permanent;
  }
}

Note: Using nginx version 1.1.19

like image 90
yorch Avatar answered Oct 25 '22 14:10

yorch


Maybe this works:

server {
  listen 80;
  server_name www.domain.net domain.net;
  location / {
    rewrite "^$" http://www.domain.com/ permanent;
  }
}
like image 20
blueberryfields Avatar answered Oct 25 '22 12:10

blueberryfields