1. This website uses cookies to improve service and provide a tailored user experience. By using this site, you agree to this use. See our Cookie Policy.
    Dismiss Notice

Help needed with GEO redirecting script

Discussion in 'HTML & JavaScript' started by JohnKowalski, May 14, 2020.

Tags:
  1. JohnKowalski

    JohnKowalski Junior Member

    Joined:
    Jan 11, 2016
    Messages:
    144
    Likes Received:
    74
    Gender:
    Male
    Location:
    Europe
    Hello, this script below redirects US users to google.us(com) and the rest of the world to google.be

    How to modify the script so it could redirect to different countries websites?


    example:

    traffic from Italy redirects to google.it
    traffic from Spain redirects to google.es
    traffic from France redirects to google.fr
    traffic from Germany redirects to google.de

    etc.



    [​IMG]



    I,ve tried to modify the code with help sources available on the internet, but nothing seems to work so far.


    Sorry had to make a screen shot, there was a popup warning not allowing me to place it inside code tag.


    Thanks in advance for any kind of help.
     
  2. darnoldi

    darnoldi Registered Member

    Joined:
    Jan 6, 2011
    Messages:
    71
    Likes Received:
    25
    Try this:
    Code:
    if(geoplugin_countryCode()=='US'){
        location.href = "https://google.us";
    }elseif(geoplugin_countryCode()=='IT'){
        location.href = "https://google.it";
    }elseif(geoplugin_countryCode()=='ES'){
        location.href = "https://google.es";
    }elseif(geoplugin_countryCode()=='FR'){
        location.href = "https://google.fr";
    }elseif(geoplugin_countryCode()=='DE'){
        location.href = "https://google.de";
    }else{
    location.href = "https://google.com";
    }
    NB! I have not tested this, just type it in notepad quickly.

    You will need to confirm the country codes are correct, I have just guessed.
    The last location will be the default that will be redirected to if none of the other country codes match.



    HTH
     
    • Thanks Thanks x 1
  3. JohnKowalski

    JohnKowalski Junior Member

    Joined:
    Jan 11, 2016
    Messages:
    144
    Likes Received:
    74
    Gender:
    Male
    Location:
    Europe
    Thanks for giving a shot but unfortunately it doesn't work. No redirection to any country now. I thing I might have tried it too before :)
     
  4. darnoldi

    darnoldi Registered Member

    Joined:
    Jan 6, 2011
    Messages:
    71
    Likes Received:
    25
    If the redirects work with your original code, and you confirmed that the country codes are valid, this really should work.
    Not sure of your programming skills, so apologies if this is something that is obvious to you, but did you replace all the code within the script tags with the code I pasted?
     
  5. Meerakat

    Meerakat Jr. VIP Jr. VIP

    Joined:
    Sep 9, 2019
    Messages:
    540
    Likes Received:
    296
    Gender:
    Male
    This should 100% work.
     
  6. JohnKowalski

    JohnKowalski Junior Member

    Joined:
    Jan 11, 2016
    Messages:
    144
    Likes Received:
    74
    Gender:
    Male
    Location:
    Europe
    Sorry guys the screenshot again, but cloudfare doesn't allow me to post with code tags.

    My programming skils are very low, and this is how the code looks like now with the help of @darnoldi and it doesn't work.

    [​IMG]
     
  7. TashaJ

    TashaJ Registered Member

    Joined:
    Jul 9, 2019
    Messages:
    59
    Likes Received:
    12
    Are there any error codes in console. Maybe the plugin hasnt got them defined and cause the script to fail?
     
    • Thanks Thanks x 1
  8. fwezz

    fwezz Newbie

    Joined:
    Apr 21, 2020
    Messages:
    15
    Likes Received:
    8
    Gender:
    Male
    Occupation:
    Full Stack Web Dev. & DevOps
    Location:
    Romania
    It might be because of `Mixed Content` so your website might have SSL (https://) and the plugin url only has (http://)
     
    • Thanks Thanks x 1
    Last edited by a moderator: May 14, 2020
  9. fwezz

    fwezz Newbie

    Joined:
    Apr 21, 2020
    Messages:
    15
    Likes Received:
    8
    Gender:
    Male
    Occupation:
    Full Stack Web Dev. & DevOps
    Location:
    Romania
    I couldn't edit on the post so i'll reply here.


    HTML:
        
    $.get("https://wtfismyip.com/json", function(response) {
            var cc = response.YourFuckingCountryCode;
    if(cc === 'US'){
        location.href = "https://google.us";
    }else if(cc === 'IT'){
        location.href = "https://google.it";
    }else if(cc === 'ES'){
        location.href = "https://google.es";
    }else if(cc === 'FR'){
        location.href = "https://google.fr";
    }else if(cc === 'DE'){
        location.href = "https://google.de";
    }else{
        location.href = "https://google.com";
    }
       });
    PS: DON'T FORGET TO ADD JQUERY
     
    • Thanks Thanks x 1
  10. fwezz

    fwezz Newbie

    Joined:
    Apr 21, 2020
    Messages:
    15
    Likes Received:
    8
    Gender:
    Male
    Occupation:
    Full Stack Web Dev. & DevOps
    Location:
    Romania
    else if*
     
  11. darnoldi

    darnoldi Registered Member

    Joined:
    Jan 6, 2011
    Messages:
    71
    Likes Received:
    25
    Ok so there was a bug in my code.

    The following is tested and works 100%, but cannot talk to how cloudflare would affect it.

    [​IMG]
     
    • Thanks Thanks x 1
  12. Sebastiann

    Sebastiann Newbie

    Joined:
    Apr 27, 2020
    Messages:
    34
    Likes Received:
    26
    Gender:
    Male
    Try this:

    https://codepen.io/sebastiann/pen/XWmPmPO


    [​IMG]
     
    • Thanks Thanks x 1
  13. Sebastiann

    Sebastiann Newbie

    Joined:
    Apr 27, 2020
    Messages:
    34
    Likes Received:
    26
    Gender:
    Male
    Out of interest is there any reason you're doing this client-side rather than server-side?

    Also this:
    "
    If geoplugin.net was responding perfectly, then stopped, then you have gone over the free lookup limit of 120 requests per minute.

    We automatically block all requests coming from an IP or a domain name if the number of requests exceeds 120 lookups per minute. This is explained in our https://www.geoplugin.com/aup. This block comes in the form of a HTTP/1.1 403 Forbidden reply from geoplugin.net

    This block is automatically removed 1 hour after the last time your server stopped sending more than 120 requests a minute.
    "
     
    • Thanks Thanks x 1
  14. JohnKowalski

    JohnKowalski Junior Member

    Joined:
    Jan 11, 2016
    Messages:
    144
    Likes Received:
    74
    Gender:
    Male
    Location:
    Europe
    @Sebastiann thank you! The script you shared works pefectly. I'm doing this client-side because, I will use it for affiliate offers, this way it's easy for me to modify the code on the spot , at list for now.

    Thanks very much to all of you guys for your help!

    You're all Great !!!!!!!!!!!!

    :)