Amirol Ahmad
Amirol Ahmad

Reputation: 542

Redirecting viewers to other site based on country geoip

let's say I have a website (www.abc.com/featured/abc) and wanted to redirecting (only that link) the viewer from other country except Malaysia to youtube video.. But www.abc.com is still worldwide. And yes, my server is already compiling with geoip module.

here is what i did in etc/nginx/sites-available/abc.com

location featured/abc/ {
  if ($geoip_country_code != "MY") {
    rewrite ^ https://www.youtube.com/watch?v=MaMuQPmzrrU;
  }
}

but it still not working. Do I did somewhere wrong in the code

Upvotes: 2

Views: 6137

Answers (1)

Aleksey Deryagin
Aleksey Deryagin

Reputation: 2675

Try map variable like this:

http {
...
map $geoip_country_code $georedirect {
  default 0;
  MY 1;
}
...
}

server {
...
location /featured/abc/ {
  if ($georedirect) {
    return 301 https://www.youtube.com/watch?v=MaMuQPmzrrU;
  }
}
...
}

More info: Module ngx_http_map_module

Upvotes: 3

Related Questions