language detection by subdomain - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: language detection by subdomain (/showthread.php?tid=8593) |
language detection by subdomain - El Forum - 05-23-2008 [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 language detection by subdomain - El Forum - 05-23-2008 [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. language detection by subdomain - El Forum - 05-24-2008 [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'])); 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: /* Once you have $language, you can assign it to a session varibale, load language files, etc. |