Forum Moderators: coopster

Message Too Old, No Replies

Do you direct visitors based on language?

PHP or .htaccess or something else?

         

soquinn

11:58 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



Anyone know if this is a good method in PHP?

<?php
if(strstr($HTTP_ACCEPT_LANGUAGE,"en-us")) {
Header("Location: [test1.com");...]
}
elseif(strstr($HTTP_ACCEPT_LANGUAGE,"fr")) {
Header("Location: [test2.com");...]
}
else {Header("Location: [test.com");...]
}
?>

Or is something in .htaccess better?

coopster

12:09 am on Mar 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could use an approach like that, but if you really want to do it right you should look into Content Negotiation. For Apache, it's here:

[httpd.apache.org...]

soquinn

7:52 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



Thanks... I'll have a look.

encyclo

8:22 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I got the following with help from this board, but I can't find the original thread:

<?php
// PHP detect language script.

//check first to see if they've been nice and
//set the language
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {

//grab all the languages
$langs=explode(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]);

//start going through each one
foreach ($langs as $value) {

//select only the first two letters
$choice=substr($value,0,2);

//redirect to the different language page
//based on their first chosen language
switch ($choice) {
case "en":
header("HTTP/1.1 301 Moved Permanently");
header("Location: ./english.html");
exit;
break;
case "fr":
header("HTTP/1.1 301 Moved Permanently");
Header("Location: ./french.html");
exit;
break;
default:
header("HTTP/1.1 301 Moved Permanently");
Header("Location: ./french.html");
exit;
}
}
}

//If the language is not set then use this
//as default
else {
header("HTTP/1.1 301 Moved Permanently");
header("Location: ./french.html");
exit;
}
?>

The idea with the above script is not to automatically redirect every page, just to detect the language of the user when hitting the index page. You might want to develop this further, perhaps by adding a cookie and a language preference setting.