Welcome Guest, Not a member yet? Register   Sign In
language detection by subdomain
#3

[eluser]deck1[/eluser]
This is my approach:

First of all, at top of index.php I filter all request without subdomain. trying to avoid duplicate pages e.g.: domain.com vs www.domain.com:

Code:
$safe_http_host = strtolower(preg_replace("/[^a-zA-Z0-9\_\-\.]/", "" , $_SERVER['HTTP_HOST']));
$request_uri    = $_SERVER['REQUEST_URI'];
$host_parts     = explode('.', $safe_http_host);
$count_parts    = count($host_parts);

/*
* It's a first level domain without www
* ie: domain.com. Redirect to www.domain.com
*/
if ($count_parts == 2)
{
    $redirect_url    = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
    $redirect_url    .= "://www.".$safe_http_host;
    $redirect_url    .= $request_uri;

    @header("HTTP/1.1 301 Moved Permanently", TRUE, 301);
    @header("Location: " . $redirect_url);
    exit;
}

Well, now we have a clean subdomain. The next step is to detect language in url. I use a hook (i use language code in first segment), but the process is the same:

Code:
/*
* Language code in subdomain
*/
$safe_http_host = strtolower(preg_replace("/[^a-zA-Z0-9\_\-\.]/", "" , $_SERVER['HTTP_HOST']));
$host_parts     = explode('.', $safe_http_host);

$language_code  = $host_parts[0];

$language_array = array('www' => 'portuguese',
                        'en' => 'english');
                        
if(array_key_exists($language_code, $language_array))
{
    $language = $language_array[$language_code];
} else
{
    //redirect to www
}

Once you have $language, you can assign it to a session varibale, load language files, etc.


Messages In This Thread
language detection by subdomain - by El Forum - 05-23-2008, 09:38 AM
language detection by subdomain - by El Forum - 05-23-2008, 03:50 PM
language detection by subdomain - by El Forum - 05-24-2008, 04:17 AM



Theme © iAndrew 2016 - Forum software by © MyBB