5

I have two domains www.domain.com and www.domain.com.tw. I want the user to be redirected to the closest server, but if they want to view the other server, they can by clicking a link in the site.

I tried to do it with a cookie, but it doesn't seem to be working. I think the problem is that the cookie is set for the refering URL and not the redirected URL, so nginx doesn't even see the cookie. How can I do this? Here is my attempt so far.

server {
    listen 80;
    server_name www.domain.com.tw;

    set $redir "";
    if ($http_accept_language ~* "en") {
        set $redir "1";
    }
    if ($http_cookie ~* "noredir") {
        set $redir "";
    }
    if ($redir = "1") {
        rewrite ^ https://www.domain.com/;
    }
}
richard
  • 153
  • 1
  • 3

1 Answers1

0

As you said yourself, because cookies are domain dependent, the one set on .com will not be seen by .com.tw. So, in your redirect, you can add a parameter like ?noredir=true. Then when the .com receives that request, it can not redirect and set the cookie so it remembers.

Grumpy
  • 2,979
  • 18
  • 23