# Nginx geoip redirect based on country codes but exclude googlebot

**URL:** <https://www.ruby-forum.com/t/nginx-geoip-redirect-based-on-country-codes-but-exclude-googlebot/199260>\
**Category:** NGINX\
**Created:** [November 17, 2010, 1:56pm UTC](https://www.ruby-forum.com/t/nginx-geoip-redirect-based-on-country-codes-but-exclude-googlebot/199260 "2010-11-17T13:56:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 17, 2010, 1:56pm UTC](https://www.ruby-forum.com/t/nginx-geoip-redirect-based-on-country-codes-but-exclude-googlebot/199260/1 "2010-11-17T13:56:59Z")

</div>

Hello,

I want to redirect people to specific pages on a website according to  
their country but i dont want to redirect googlebot or other spiders,  
how can i do this with nginx ?

I have done redirection with php earlier but havent been able to exclude  
googlebot.

As i read from maxmind website, on apache its done like this

```auto
GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat

# Redirect one country
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteRule ^(.*)$ http://www.canada.com$1 [L]

# Redirect multiple countries to a single page
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA|US|MX)$
RewriteRule ^(.*)$ http://www.northamerica.com$1 [L]
```

It still does not have any info to remove googlebot from redirection.

Thanks for any help.

Posted at Nginx Forum:

> **[Nginx geoip redirect based on country codes but exclude googlebot](https://forum.nginx.org/read.php?2,151401,151401#msg-151401)**
>
> Hello, I want to redirect people to specific pages on a website according to their country but i dont want to redirect googlebot or other spiders, how can i do this with nginx ? I have done redirection with php earlier but havent been able to exclude...

---

<div class="post-metadata">

**Author:** ![st1905](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@st1905](https://www.ruby-forum.com/u/st1905)\
**Post date:** [November 17, 2010, 7:53pm UTC](https://www.ruby-forum.com/t/nginx-geoip-redirect-based-on-country-codes-but-exclude-googlebot/199260/2 "2010-11-17T19:53:24Z")

</div>

Am 17.11.2010 um 13:55 schrieb st1905:

> As i read from maxmind website, on apache its done like this
> 
> # Redirect multiple countries to a single page
> 
> RewriteEngine on  
> RewriteCond %{ENV:GEOIP\_COUNTRY\_CODE} ^(CA|US|MX)$  
> RewriteRule ^(.\*)$ [http://www.northamerica.com$1](http://www.northamerica.com$1) [L]  
> [/code]
> 
> It still does not have any info to remove googlebot from redirection.

We did it like this (not for google - you have to find their ip-space)

http {  
keepalive\_timeout 65;  
geoip\_country /usr/local/etc/geo/GeoIP.dat ;

# these are the GeoIP exceptions for your site

```
 geo $mygeo {
             default 0;
             bla/19 1;
             blu/16 1;
 }

```

then, in the server block:

```
     location / {
         include proxy-lcb-lt.conf;
         if ($geoip_country_code = "US") {
                             proxy_pass http://prod;
             }
         # these are the exceptions, defined in nginx.conf:
         if ($mygeo = "1") {
             proxy_pass http://prod;
             }
     }

```

Hope this helps.

Rainer

---

<div class="post-metadata">

**Author:** ![st1905](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@st1905](https://www.ruby-forum.com/u/st1905)\
**Post date:** [November 18, 2010, 10:54am UTC](https://www.ruby-forum.com/t/nginx-geoip-redirect-based-on-country-codes-but-exclude-googlebot/199260/3 "2010-11-18T10:54:53Z")

</div>

Hello,

Thank you for the reply, i`m going to try using this on my  
configuration, i was thinking to exclude it by referrer which googlebot  
uses but maybe using ip would work too, in fact the ips would change.

Posted at Nginx Forum:

> **[Re: Nginx geoip redirect based on country codes but exclude googlebot](https://forum.nginx.org/read.php?2,151401,151637#msg-151637)**
>
> Hello, I want to redirect people to specific pages on a website according to their country but i dont want to redirect googlebot or other spiders, how can i do this with nginx ? I have done redirection with php earlier but havent been able to exclude...

---

<div class="post-metadata">

**Author:** ![st1905](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@st1905](https://www.ruby-forum.com/u/st1905)\
**Post date:** [November 18, 2010, 3:51pm UTC](https://www.ruby-forum.com/t/nginx-geoip-redirect-based-on-country-codes-but-exclude-googlebot/199260/4 "2010-11-18T15:51:51Z")

</div>

Hello,

> Posted at Nginx Forum:  
> [http://forum.nginx.org/read.php?2,151401,151657#msg-151657](http://forum.nginx.org/read.php?2,151401,151657#msg-151657)

The GoogleBot header can be faked.See e.g.  
[http://www.zhacks.com/2010/05/06/view-restricted-website-by-cloacking-as-googlebot/](http://www.zhacks.com/2010/05/06/view-restricted-website-by-cloacking-as-googlebot/)  
I’d go for the IP range.  
Hendrik

---

<div class="post-metadata">

**Author:** ![st1905](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@st1905](https://www.ruby-forum.com/u/st1905)\
**Post date:** [November 18, 2010, 12:38pm UTC](https://www.ruby-forum.com/t/nginx-geoip-redirect-based-on-country-codes-but-exclude-googlebot/199260/5 "2010-11-18T12:38:07Z")

</div>

Hello,

Just to confirm, your configuration works fine, i have applied it today,  
still wondering if it would be better to use refferrer or ip ? Any  
suggestions ? Thank you.

Posted at Nginx Forum:

> **[Re: Nginx geoip redirect based on country codes but exclude googlebot](https://forum.nginx.org/read.php?2,151401,151657#msg-151657)**
>
> Hello, I want to redirect people to specific pages on a website according to their country but i dont want to redirect googlebot or other spiders, how can i do this with nginx ? I have done redirection with php earlier but havent been able to exclude...

---

<div class="post-metadata">

**Author:** ![st1905](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/35a633/32.png) [@st1905](https://www.ruby-forum.com/u/st1905)\
**Post date:** [November 18, 2010, 6:55pm UTC](https://www.ruby-forum.com/t/nginx-geoip-redirect-based-on-country-codes-but-exclude-googlebot/199260/6 "2010-11-18T18:55:17Z")

</div>

Thank you for that information. Using ip now.

Posted at Nginx Forum:

> **[Re: Nginx geoip redirect based on country codes but exclude googlebot](https://forum.nginx.org/read.php?2,151401,151753#msg-151753)**
>
> Hello, I want to redirect people to specific pages on a website according to their country but i dont want to redirect googlebot or other spiders, how can i do this with nginx ? I have done redirection with php earlier but havent been able to exclude...
