How to reroute to a different url identifying brow

2019-09-20 15:53发布

问题:

The Issue

We've got a ccTLD website (example.de) with subfolders for different languages, which obviously needs to be moved to example.com for proper language targeting. Since example.com belongs to the parent group company which has been 301ing all visitors to us-group.com so far and is reluctant to allow us 301ing example.de to example.com and ditch their existing 301s to us-group.com, we need to work around a bit.

The Task

  • Visitors from the North and South Americas should be rerouted to us-group.com
  • Visitors from all remaining countries (such as Europe and South East Asia) should be redirected from example.de to example.com and stay there without being rerouted to us-group.com

The Challenge

We figured that many visitors from the Americas use proxies, so rerouting by IP wouldn't be an option. The us-group.com simply would lose a lot of their previous traffic.

Would it be possible to reroute them with a PHP script with a combination of browser and system language (eg: EN-US, FR-CA, EN-CA, ES-AR, PT-BR, etc.)? If so, how could this be done?


Additional thoughts: Obviously, that's not the only thing we consider doing. Before rerouting visitors from the Americas, we would:

  1. modify the DNS settings of the example.com domain (A record: IP is going to be changed to the IP address of the German server)
  2. modify the vhost record, so the German server redirects the example.com visitors to the directory example.com/en/ (containing the English parts of the content of example.de)
  3. Reroute visitors from the Americas to the us-group.com (by browser & OS language, as mentioned above)

回答1:

I use something similar to this, this is based upon browser language though and not on system languge in any way.

if(!isset($_COOKIE['language'])){
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4);
switch($lang){
        case "en-US":
          $rURL = "www.example.com"; 
          break;
        default:
          $rURL = "www.example.de"; 
      }

      if(isset($rURL)){
          setcookie("language", $rURL);
          header("HTTP/1.1 303 See Other");
          header('Location:' . $rURL);
      }
}

You can of course change how long the cookie should be stored and so on :)

UPDATE If you want to redirect the user back to the original/detected site (If they by mistake or other way get back to the wrong domain you can simply use

if(!isset($_COOKIE['language'])){
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4);
    switch($lang){
            case "en-US":
              $rURL = "www.example.com"; 
              break;
            default:
              $rURL = "www.example.de"; 
          }

          if(isset($rURL)){
              setcookie("language", $rURL);
              header("HTTP/1.1 303 See Other");
              header('Location:' . $rURL);
          }
    }else{
         header("HTTP/1.1 303 See Other");
         header('Location:' . $_COOKIE['language']);
    }


标签: php reroute