Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect visitor by country using .htaccess

I want to dynamically redirect visitors from IPs in certain countries



8:46 pm on Jan 24, 2007 (gmt 0)

10+ Year Member

I need to 301 redirect visitors to my dot ca website from countries A and B to my dot com website. (Please note that my dot ca website is deliberately not indexed by search engines - robots.txt blocks all bots).

I realize that I need to lookup each visitor's IP address to see what country they are from. I can do this using a few options from various software providers (ie: a local database lookup with a database that needs constant updating OR using a web service where the database is stored and updated by the softare provider). I'd prefer to use the web service option.

My question: Can either option be done from within my .htaccess file? This way, I can avoid having to edit every page on the site (100+) to add a snippet of PHP code to do the IP lookup and 301 accordingly.

I think what I'm asking is - can the contents of the .htaccess file be updated dynamically (since the IP addresses/ranges of the blocked countries are constantly changing)?



12:25 am on Jan 27, 2007 (gmt 0)

10+ Year Member

I can't think of a way to do it by using external web services, but you can accomplish what you want with the maxmind mod_geoip apache extension. Google "maxmind mod_geoip". It is fantastic.

They have free and commercial versions of their database, but you will have to keep a local copy. Better that way anyway - you wouldn't want to be doing an external request for every page hit.


4:29 pm on Jan 29, 2007 (gmt 0)

10+ Year Member

Thanks sabai! A perfect solution.

Too bad Maxmind's website is so difficult to navigate because if it wasn't I would've already found this myself :)


Featured Threads

Hot Threads This Week

Hot Threads This Month