Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

My client has a very simple website with two different languages and I decided to make a separate folder for each language, for example:

domain.com/en/ domain.com/de/

But in the root folder there's no index file, only an .htaccess file that 301 redirects based on HTTP:Accept-Language. It works just fine, but the thing is that the page is indexed in Google but without a snippet and no language specified in the URL.

My questions are: Is this a good practice or should I add an index file in the root folder? Or is there any other way to tackle this problem?

share|improve this question

2 Answers 2

This may not really be an answer to your question, but the best way to make a multilingual is in most cases by using some simple PHP.

Create a folder called languages and create the files lang.code.php. Replace code with, for example EN for English, DE for German, etc.

Create a language switcher with the following code:

<?php
$lang = "en";
if( isset( $_POST["lang"] ) ) {
    $lang = $_POST["lang"];
    setcookie ( 'lang', $lang, time() + 60*60*24*30, '/', 'YOUR_DOMAIN');
    header( "Location: /" );
}
?>

No customization is needed.

Place the following PHP code on the page where you want your language switch to be.

session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];

// register the session and set the cookie
$_SESSION['lang'] = $lang;

setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}

if( $_COOKIE["lang"] == "en" ) {
$lang = 'en';
}

if( $_COOKIE["lang"] == "nl" ) {
$lang = 'nl';
}

switch ($lang) {
  case 'en':
  $lang_file = 'lang.en.php';
  break;

  default:
  $lang_file = 'lang.en.php';

}

include_once 'languages/'.$lang_file;

For every language, add a new case. Change default to the value that your default language should be.

Then, add the following HTML code to the same page as you placed the PHP code ^:

<form action="language_switcher.php" method="post">
    <select name="lang">
        <option value="en"<?php if( $_COOKIE["lang"] == "en" ) { echo " selected"; } ?>>English</option>
    </select>
    <button type="submit" class="btn btn-warning btn-small">Submit</button>
</form>

Add an option for every language that your website should have. The code assumes that your PHP language switcher is called language_switcher.php.

A language file looks like this:

/*
------------------
Language: English
------------------
*/

$lang = array();

// Head

$lang['HEADER_TITLE'] = 'Portal';

// Menu

$lang['HOME'] = 'Home';
$lang['ADMIN'] = 'Admin';

Then, replace all the text in your website with:

<?php echo $lang['VAR']; ?>

where VAR will represent a value in the language file.

It looks harder than it is. Good luck!

share|improve this answer
    
Thanks for the edit, for some reason I was not able to insert PHP code... –  William D. Edwards Jul 23 at 18:27

As far as I know, there is no specific needs to put your files on root. If you think your users will get benefited with this practice, go for it.

My Suggestion:

Keep your default language on your root domain. For example your main target audience is En, keep English version at root.

For other language, create sub-folders.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.