Question

NGINX GeoIP Redirect and Search Engine Bots

Posted June 10, 2020 1.5k views
NginxWordPress

I have managed to get NGINX GEOIP by Country to redirect, the following is what I have done:

nginx.conf

http {

        ##
        # Add GeoIP to NGINX
        ##
        geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
        geoip_country /usr/share/GeoIP/GeoIP.dat;
...
}

in my host file I placed the following

map $geoip_city_country_code $closest_server {
    default example.com;
    NZ      nz.example.com;
    CA      ca.example.com;
    US      us.example.com;
    GB      gb.example.com;
}

server {
...
server_name example.com nz.example.com ca.example.com us.example.com gb.example.com;
...
if ($closest_server != $host) {
    rewrite ^ $scheme://$closest_server$request_uri break;
}

What I need it to do is to redirect to the closest server but also allow the user to select another international subdomain and not redirect.

Secondly, I need to exclude search engine bots to be excluded from these rules.

Any assistance would be great.

edited by KFSys

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
2 answers

@bris4171 did you solve this? I have the same issue, how to allow the user to select another international subdomain and not redirect.

  • I didn’t solve it through nginx, but I did by using a Wordpress plugin called ‘WP GeoIP Redirect Rules’ I got it from code canyon and it works a treat. So if you are using Wordpress Highly recommend it.

@bris4171 I found a solution, how to allow the user to select another international subdomain and not redirect - with http referer.

map $http_cf_ipcountry $regional_server {
    default            "en.example.org";
    "US"               "example.org";
}


   if ($regional_server != $host) {
      set $lang A;
   }

   if ($http_referer !~* "example.") {
      set $lang "${lang}B";
   }

   if ($lang = AB) {
      rewrite ^ $scheme://$regional_server$request_uri break;
   }

$http_cf_ipcountry - I use cloudflare geoip solution

Now users can change subdomain