Joomla!, Wordpress tips for building your site

Redirect Loop by qTranslate on Wordpress 3.0

Published on| July 23rd, 2010 | 1 Comment.

It has passed a few weeks after WordPress3.0 released.
Now, many plug-in developers has make work compatibility to 3.0.

So I think that I want to update WordPress from 2.8 to 3.0. And I tried it, but I had a few problems.

One is problem of qTranslate.
It is problem that is occurred redirect loop by qTranslate.
You might not show first page of your site by this problem.
If you have same problem, this post might help you.

How do this problem occur?

If you access to the first page of your site, you will see following message.


This problem occurs by some factors.
It is that your web browser is set ‘unused cookie’ and following factors.

Checked – Detect the language of the browser and redirect accordingly.
Unchecked Hide URL language information for default language.


If your web browser is set ‘use cookie’, this problem will not occur.

Why do this problem occur?

Perhaps, you notice that qTransrate will try redirect to following URL by this settings of qTransrate.
Example, default language is japanease.

http://wwww.example.com/

          |
          V

http://wwww.example.com/ja

          |
          V

http://wwww.example.com/

How do this problem resolve?

If you edit ‘qtranslate_core.php’, you might resolve this problem.

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
	// detect language and forward if needed
	if($q_config['detect_browser_language'] && $q_config['url_info']['redirect'] && !isset($_COOKIE['qtrans_cookie_test']) && $q_config['url_info']['language'] == $q_config['default_language']) {
		$prefered_languages = array();
		if(isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) && preg_match_all("#([^;,]+)(;[^,0-9]*([0-9\.]+)[^,]*)?#i",$_SERVER["HTTP_ACCEPT_LANGUAGE"], $matches, PREG_SET_ORDER)) {
			$priority = 1.0;
			foreach($matches as $match) {
				if(!isset($match[3])) {
					$pr = $priority;
					$priority -= 0.001;
				} else {
					$pr = floatval($match[3]);
				}
				$prefered_languages[$match[1]] = $pr;
			}
			arsort($prefered_languages, SORT_NUMERIC);
			foreach($prefered_languages as $language => $priority) {
				if(qtrans_isEnabled($language)) {
					if($q_config['hide_default_language'] && $language == $q_config['default_language']) break;
					$target = qtrans_convertURL(get_option('home'),$language);
					header("Location: ".$target);
					exit;
				}
			}
		}
	}

Line 87, this means some factors that occur this problem.

!isset($_COOKIE[‘qtrans_cookie_test’])
= this means that your web browser is set ‘unused cookie’.

$q_config[‘detect_browser_language’]
= ■ Checked – Detect the language of the browser and redirect accordingly.

$q_config[‘url_info’][‘redirect’]
= □ Unchecked Hide URL language information for default language.

I think that this problem might resolve by checking now URL and redirect URL same($target at Line 105).

Let’s try insert check program.

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	// detect language and forward if needed
	if($q_config['detect_browser_language'] && $q_config['url_info']['redirect'] && !isset($_COOKIE['qtrans_cookie_test']) && $q_config['url_info']['language'] == $q_config['default_language']) {
		$prefered_languages = array();
		if(isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) && preg_match_all("#([^;,]+)(;[^,0-9]*([0-9\.]+)[^,]*)?#i",$_SERVER["HTTP_ACCEPT_LANGUAGE"], $matches, PREG_SET_ORDER)) {
			$priority = 1.0;
			foreach($matches as $match) {
				if(!isset($match[3])) {
					$pr = $priority;
					$priority -= 0.001;
				} else {
					$pr = floatval($match[3]);
				}
				$prefered_languages[$match[1]] = $pr;
			}
			arsort($prefered_languages, SORT_NUMERIC);
			foreach($prefered_languages as $language => $priority) {
				if(qtrans_isEnabled($language)) {
					if($q_config['hide_default_language'] && $language == $q_config['default_language']) break;
					$target = qtrans_convertURL(get_option('home'),$language);
 
$http = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$npos = strpos($target,'//');
if($npos!==false) {
	$target_chk = substr($target,$npos+2);
} else {
	$target_chk = $target;
}
if($target_chk == $http) break;
 
					header("Location: ".$target);
					exit;
				}
			}
		}
	}

Lines 105 – 114 , insert check program URL.

It is simple.
if now URL and redirect URL same, redirect will be ignored.

This check process work for my problem.

If you do not want to edit ‘qtranslate_core.php’, you should reset followings.

Checked – Detect the language of the browser and redirect accordingly.
Unchecked Hide URL language information for default language.






Comments

One Response to “Redirect Loop by qTranslate on WordPress 3.0”

  1. Adam
    October 7th, 2011 @ 23:04:40

    Hi,
    Thank you for your post – I’m surprised to be first to say thank you.

    I’m using qTranslate in version 2.5.24 and there is slight difference in the code of qtranslate_core.php.

    Basically the Redirect itself (and exit) is done after the loop so break does not work anymore to prevent redirect. Simple fix is to introduce a flag (I call it $redirect_corrupt), which is risen once the faulty redirect is detected by your code. Then the flag is being checked when redirect is about to be applied.

    Cheers & thanks,
    Adam

Leave a Reply








Transrator

Recent Posts

Categories

Tag Cloud

execute remove フロントページ 投稿ページ トップページ install WIndows Note Convert META generator 日付 donwload file manage multibyte utf-8 unicode shiftjis euc console サイトマップ 問題 ParmaLink Redirect パーマリンク はみ出る pre テンプレート テーマ タグクラウド マルチランゲージ リダイレクト PHP(タグ) タグ table control HTML(タグ) コマンド 国際化(翻訳) SQLite(タグ) MySQL(タグ) qTranslate プラグイン(タグ) Wordpress(タグ)

Links

Site Description

Joomla!, Wordpress as CMS using a site building, site management, software usage to describe the development tips.

  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ