Home > Software engineering >  Apache Rewrite Rule based on GeoIP
Apache Rewrite Rule based on GeoIP

Time:01-27

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

CodePudding user response:

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.

  •  Tags:  
  • Related