0

I have a multi regional based site and I redirect users according to the IP address.Following is the code to redirect the users according to their country. But this code prevent Googlebot from reaching the pages other than USA. What should be the ideal structure so that googlebot may visit every pages and users are redirected as per their IP address? Thanks in advance.

if ($country == "IN")
      {
    // do nothing


      }

    else if ($country == "BD" ")
      {
      header( 'Location:https://www.exmple.com/directory/bangladesh/index.php');

      }

    else if ($country  == "PK"  )
      {
      header('Location:https://www.exmple.com/directory/pakistan/index.php');
     }
    else if ($country  == "LK" )
      {
      header('Location:https://www.exmple.com/directory/srilanka/index.php');
     }
    else if ($country  == "US" )
      {
      header('Location:https://www.exmple.com/directory/usa/index.php');
     }
    else if ($country  == "CA")
      {
      header('Location:https://www.exmple.com/directory/canada/index.php');
     }
    else if ($country  == "GB")
      {
      header('Location:https://www.exmple.com/directory/uk/index.php');
     }
    else if ($country  == "NG")
      {
      header('Location:https://www.exmple.com/directory/nigeria/index.php');
     }
    else
      {
         header( 'Location:https://www.exmple.com/directory/global/index.php');
     }
rns
  • 92
  • 10
  • Perhaps set a session variable when someone first visits and redirect them to the country you THINK they want to look at, but if that variable is set when they return to the home page, they avoid redirects. Because it's not just for Googlebot. What if a user in Canada wants to see the directory for Great Britain? They can't? – Lucas Krupinski May 08 '17 at 11:29
  • Please give the structure you wanted to say. – rns May 08 '17 at 11:43
  • No structural changes, just no automatic redirects based on IP address. Or if you do that, only redirect the first time, and let visitors traverse your entire site after that, rather than your current strategy of guessing what data a visitor (or googlebot) wants to see. – Lucas Krupinski May 08 '17 at 11:50
  • According to you if I set my homepage to redirect users, how googlebot will crawl it? – rns May 08 '17 at 11:56
  • Googlebot IS a user. If google isn't able to crawl your site, that means your users will have issues as well, as you are already seeing. What country are YOU in? What happens when YOU try to access your directory for another country? This isn't a googlebot specific issue, this is operational for all users. – Lucas Krupinski May 08 '17 at 12:00
  • I think you are right. I will look into the matter as per your advice. Thanks a lot. – rns May 08 '17 at 12:05

2 Answers2

0

If USA-based users are blocked from accessing page, but allow visitors from other country, say India to see it, server would block the spider that appears to be coming from the USA. So following modifications of code would work:

if ($country == "US")
      {
    // do nothing


      }

    else if ($country == "BD" ")
      {
      header( 'Location:https://www.exmple.com/directory/bangladesh/index.php');

      }

    else if ($country  == "PK"  )
      {
      header('Location:https://www.exmple.com/directory/pakistan/index.php');
     }
    else if ($country  == "LK" )
      {
      header('Location:https://www.exmple.com/directory/srilanka/index.php');
     }
    else if ($country  == "IN" )
      {
      header('Location:https://www.exmple.com/directory/india/index.php');
     }
    else if ($country  == "CA")
      {
      header('Location:https://www.exmple.com/directory/canada/index.php');
     }
    else if ($country  == "GB")
      {
      header('Location:https://www.exmple.com/directory/uk/index.php');
     }
    else if ($country  == "NG")
      {
      header('Location:https://www.exmple.com/directory/nigeria/index.php');
     }
    else
      {
         header( 'Location:https://www.exmple.com/directory/global/index.php');
     }
rns
  • 92
  • 10
0

You can use gethostbyaddr to check if the request comes from a Google crawler, then with that information you can decide where to redirect the user.

// Method will return "crawl-66-249-66-1.googlebot.com"
$host = gethostbyaddr('66.249.66.1');

// Check if the host contains a google domain.
$isGoogle = (strpos($host, 'googlebot.com') !== false);

Be cognizant of Google's policy about redirecting its bots to different pages other than the intended user pages, I'm not too sure if that's frowned upon by them.

If you want to detect more crawlers other than google, search for the IP ranges of other web crawlers.

To get the IP address of the user check out this answer: https://stackoverflow.com/a/15699240/3421225

Community
  • 1
  • 1
Carlos Granados
  • 407
  • 4
  • 14
  • So that's google. What about bing and every other crawler? What about users who want to see directories for other countries, or people using VPNs that show them as visiting from countries besides their own. And yes, I'm pretty sure google hates when their crawler is shown a completely different page than an ordinary visitor... – Lucas Krupinski May 08 '17 at 15:01
  • @LucasKrupinski you're going to have to check that out yourself. You know the function that gets you there. It's not difficult, just look up the IP ranges. I found this: http://ipinfodb.com/robots-ip-address-ranges.php – Carlos Granados May 08 '17 at 15:18