Htaccess redirect based on country of origin

21,107

Solution 1

Maybe this link (how to redirect domain according to country IP address) can help you.

I think, however, that this is not a good practice. Redirection by language or country is quite strict. It is better preferred:

  • A message at the top of your site that influence the visitor to go to the page built with his language.
  • A system that can easily change the language (flags)

This is only my opinion.

Solution 2

Yes, we used to filter visitors from Nigeria by .htaccess:

RewriteEngine On

RewriteCond %{ENV:IP2LOCATION_COUNTRY_SHORT} ^NG$
RewriteRule ^(.*)$ http://www.google.com [L]

You can generate your own at http://ip2location.com/free/visitor-redirection .

Share:
21,107

Related videos on Youtube

DanA83
Author by

DanA83

I enjoy solving seemingly hard problems by combining a few simple freely available solutions.

Updated on September 18, 2022

Comments

  • DanA83
    DanA83 over 1 year

    Can a rule that filters and redirect traffic based on country of origin be implemented in .htaccess?

    Can it be implemented in any other way then by an IP address? I need the solution to be as accurate as possible.

    • closetnoc
      closetnoc about 10 years
      I only know of using the .htaccess but that may require a large(ish) number of rules to cover all of the IP address ranges. What countries are you interested in? That may help someone to answer your question. Push comes to shove, I can perhaps see what IP address ranges exist for any country and create an example rewrite rule(s) that you can try. I expect there may be a better way though. I have no idea how people do this other than .htaccess.
  • dhaupin
    dhaupin about 9 years
    You need an Apache module to use this htaccess: ip2location.com/developers/apache