1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Using htaccess to redirect all non U.S. traffic

Discussion in 'Apache' started by Abbadox, Oct 28, 2007.

  1. #1
    Can someone help me - what is the best way to redirect all non U.S. traffic to another domain.

    I have been searching google and cannot decide which is the best way to go so I am looking for suggestions?.
     
    Abbadox, Oct 28, 2007 IP
  2. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you would be able to achieve this using Apache rewrite rules but I'm pretty sure it would be sub-optimal.

    The way I would do it would be to use MaxMind's GeoIP database with PHP and MySQL. MaxMind have two products for GeoIP... one is free, the other is £12 per month. The free one puts ISPs like AOL in America regardless of where the IP is actually located. The paid one is really quite good and even managed to locate our datacentre down to about 5 miles. The free one is accurate for about 90% of all IPs, the paid one is accurate for about 99%

    There is a reference implementation in PHP on their website and I found a tutorial on how to import the .csv version of the database into MySQL. You could always use the .dat file as they do in their example but my preference is to have a database stored in a dedicated database server.

    Make sure to have a way for users to get back to the US domain and stay there, because no Geo -> IP lookup is going to be 100% accurate and you might send one the wrong way.
     
    Ladadadada, Oct 29, 2007 IP
  3. Abbadox

    Abbadox Active Member

    Messages:
    1,279
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Thanks for the info, I have been looking at a couple of other databases and will check out your suggestion also. :)


     
    Abbadox, Oct 29, 2007 IP
  4. Cybernaut

    Cybernaut Peon

    Messages:
    408
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Maxmind also provides an apache module for its GeoIP database. Just load the module and use htaccess redirects using set geoip environment vars.
     
    Cybernaut, Oct 30, 2007 IP
  5. Abbadox

    Abbadox Active Member

    Messages:
    1,279
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    90
    #5
    I will check it out, Thanks! :)
     
    Abbadox, Oct 30, 2007 IP
  6. Sebastian Rametta

    Sebastian Rametta Peon

    Messages:
    13
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yes, maxmind is in my opinion the best route. You can use their free country database.

    1) download max mind, compile.
    2) add entires into apache httpd.conf file to use the new module
    3) decide which countries you wish to block and you can add them into a .htaccess file like this (this is what I block):

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

    SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
    SetEnvIf GEOIP_COUNTRY_CODE ID BlockCountry
    SetEnvIf GEOIP_COUNTRY_CODE NG BlockCountry
    SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry
    # ... place more countries here

    Deny from env=BlockCountry
     
    Sebastian Rametta, Nov 4, 2007 IP
  7. Kezi

    Kezi Active Member

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #7
    This is very useful Sebastian - thank you for posting it.

    One thing, how would you adjust the code, so that instead of denying people from those countries, it redirected them to another website please ?

    Any help appreciated.

    Kezi.
     
    Kezi, Jul 28, 2008 IP
  8. PeterBH

    PeterBH Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Just in-case anyone else finds this thread looking for the same answer here you go


    GeoIPEnable On
    GeoIPDBFile /path/to/GeoIP.dat


    # Redirects ALL countries except UK/GB
    RewriteEngine on
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^GB
    RewriteRule ^(.*)$ http://www.your_redirect_url.com$1 [L]
     
    PeterBH, Jul 20, 2010 IP
  9. Kezi

    Kezi Active Member

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #9
    Many thanks Peter - it's much appreciated. I thought there had to be someone that knew the answer ;-)
     
    Kezi, Jul 20, 2010 IP