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

[eluser]miguelp[/eluser]
Hi, i'm having a hard time doing a language selection using the subdomain.

what i wanna do is simple, imagine that the default language for a website or app is english "en", if the user hit www.mysite.com it loads the default.

Now what i wanna do is if the user click on the portuguese language link it will be redirect to http://pt.mysite.com, or if the language is german it get redirected to http://de.mysite.com, this part is easy, my problem is using the installation of CI on the root, how do i detect the subdomain so i can load the apropriate language content from the database?

what i wanna have is something like:
default: mysite.com
other languages: language.mysite.com

But allways use the main CI installation from the domain root.

Can anyone help me out here?

Thanks in advance
#2

[eluser]Unknown[/eluser]
Hey Miguel.

After reading your post, i remembered a script i scripted 4 years ago.
It's a Virtual SubDomain script, that pretty much do what you need.

So far i can tell from the script you pretty much only have to modify line 16 and 17, that determines what happens if a "subdomain" is detected, and that subdomain exist as a folder in your root.

You have to rename your current index.php file in CI to index2.php as this script needs to be the very first script to load.

Here's the code, hope you can use it.

<?php

$host = $_SERVER["HTTP_HOST"];

if( substr($host,0,4) == 'www.') {
$host = substr($host,4);
}

list ($sub, $domain, $end) = explode('.', $host);

if($end== "" OR $sub == "www") {
header("Location: index2.php");
exit;
}
else {
if(is_dir($sub)) {
header("Location: http://www.$domain.$end/$sub/");
exit;
}
else {
header("Location: index2.php");
exit;
}
}

?>

Regards Supremacy.
#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.




Theme © iAndrew 2016 - Forum software by © MyBB