Koralkloud
Koralkloud

Reputation: 444

Apache Rewrite Rule based on GeoIP

Currently, I am trying to redirect the domain based on GeoIP. I have already installed the GeoIP module. I am having the below domain.conf file. The issue is that the login is working fine as expected like my example.com will be default for users in India and us.example.com will be the domain when US people access the site. but currently getting

redirected you too many times. 

Note: the domain is redirecting as expected but getting the above error. I have used reverse proxy since my application is running in docker on port 8080.

It would be great if someone could give the right apache rewrite rule.

<VirtualHost *:80>
      ServerName example.com
       ServerAlias example.com us.example.com
       ProxyRequests Off
      ProxyPreserveHost On
      ProxyPass / http://localhost:8080/
      ProxyPassReverse / http://localhost:8080/

      GeoIPEnable On
      GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
      GeoIPScanProxyHeaders On


      RewriteEngine on
      RewriteCond "%{ENV:GEOIP_COUNTRY_CODE}" ^US$
      RewriteRule http://us.example.com$1


      LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

      ErrorLog ${APACHE_LOG_DIR}/example.com-error_log
      CustomLog ${APACHE_LOG_DIR}/example.com-access_log common
</VirtualHost>

Thanks in Advanace

Upvotes: 0

Views: 634

Answers (1)

Example person
Example person

Reputation: 3346

Try:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^us\.example\.com$ [NC]
RewriteCond "%{ENV:GEOIP_COUNTRY_CODE}" ^US$
RewriteRule ^ http://us.example.com [R=302,L]

You need to restart apache, and also make sure to clear browser cache before trying to see if the above works.

Edit:

The OP wanted to redirect all the users that are not from India, to example.co.

Here is the solution:

RewriteEngine On
RewriteCond %{HTTP_HOST} !(?:^|\.)example\.co$
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^IN$
RewriteRule ^ http://example.co [R=302,L]

Upvotes: 1

Related Questions