6

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!

2 Answers 2

10

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

0
2

Maybe this works:

server {
  listen 80;
  server_name www.domain.net domain.net;
  location / {
    rewrite "^$" http://www.domain.com/ permanent;
  }
}
2
  • 1
    needs to have http:// in the URL: location = / { rewrite ^ http://example.com permanent; }
    – yorch
    Mar 21, 2014 at 15:02
  • ^ is not recommended. "This is less efficient than the equivalent return directive, because it requires NGINX to process a regular expression, albeit a simple one (the caret [ ^ ], which matches the complete original URL)." nginx.com/blog/creating-nginx-rewrite-rules
    – jrc
    Nov 26, 2016 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.