How to configure .htaccess redirects based on Geo IP
by bernt & torsten
Recently I had a request, asking how can we set up redirects by country to have country-specific landing pages. This can be done by installing mod_geoip, you can download GeoIP legacy Apache module from GITHUB – compile the source on your environment and configure. Or you could install from a repository.
For ubuntu you can run the following command:
sudo apt-get install php5-geoip php5-dev libgeoip-dev
Then, run the following command:
sudo pecl install geoip
Once the installation is complete, it will probably tell you that an extension= line cannot be found in your php.ini file. Let’s find your php.ini file and add the required lines:
sudo nano /etc/php5/apache2/php.ini
This will open your php.ini, we have to add “extension=geoip.so” to the [PHP] section:
[PHP] ;AFTER THE PHP SECTION NOT BEFORE extension=geoip.so and restart Apache:
sudo service apache2 restart
Now create a .htaccess
file. For example, if you want to block clients from Russia and China:
SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry Deny from env=BlockCountry
If you want to redirect based on the country using mod_rewrite in combination with mod_geoip, your .htaccess file could look like this:
RewriteEngine on RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(NL|BE)$ RewriteRule ^(.*)$ http://www.mydomain.com/nl/$1 [L]
For more examples take a look at the website of MaxMind
That’s it – don’t forget to comment below.
The Human Brain and Its Role in Creativity, Memory, and AI
The human brain is a marvel of evolution, a universe within, teeming with thoughts, memories,...
What’s the Environmental Cost of Your Online Activities? From AI Queries to Streaming Video
Scrolling through social media, searching on Google, or chatting with an AI feels like a...
A Poem: Too Old to Be Seen, Too Wise to Be Broken
They saw my years, not the code I wrote,
Dismissed the knowledge I carried, remote.