Remove the ?lang= parameter in multilingual websites

Don't you hate the ?lang=EN or ?lang=NL parameter that is added by WebsiteBaker when you change to another language?

WebsiteBaker will detect the page you are requesting is in a different language than the current (or default) language.
When it is detected it will reload the url with the ?lang= parameter.
( For some reason it also uses the 301 status too what tells the searchengines the page is in another location, and it will reload the same url?!?!  strange!!! )

Funny thing is, this is not needed at all. A better solution is available since WebsiteBaker version 2.7 (or even before).

WebsiteBaker also checks if the session parameter 'LANGUAGE' is set, and will react on that session variable too.

So, instead of reloading the page with this ?lang= parameter, it is also fine to set the session variable and just reload the current url without any extra's.

The fix...

Open the file /framework/class.frontend.php end find the function get_page_details().
In that function replace this block:

// Check if the page language is also the selected language. If not send headers again.
if ($this->page['language']!=LANGUAGE) {
    if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { // check if there is an query-string
        header("HTTP/1.1 301 Moved Permanently"); // ADDED
        header('Location: '.$this->page_link($this->page['link']).'?'.$_SERVER['QUERY_STRING'].'&lang='.$this->page['language']);
    } else {
        header("HTTP/1.1 301 Moved Permanently"); // ADDED
        header('Location: '.$this->page_link($this->page['link']).'?lang='.$this->page['language']);
    }
    exit();
}


with these lines

// Check if the page language is also the selected language. If not send headers again. 
if ($this->page['language']!=LANGUAGE) {
    $_SESSION['LANGUAGE'] = $this->page['language'];
    if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { // check if there is an query-string
        header('Location: '.$this->page_link($this->page['link']).'?'.$_SERVER['QUERY_STRING']);
    } else {
        header('Location: '.$this->page_link($this->page['link']));
    }
    exit();
}


Now the ?lang= parameter will not be used anymore, and no 301 status is used (the default 302 is used).

This will work fine in WebsiteBaker 2.7, 2.8.* and also with the new multiLang functions of WebsiteBaker 2.11.

This article is tagged with:
PHP WebsiteBaker Tricks

Comments on this article


Due to large amounts of spam comments, the comments are disabled!