canonicalプラグインの動作

  • 2021-02-25時点
  • #canonical プラグインのテスト

動作を確認してみる

存在しないページ名は、

#canonical(仮想化);
が次の様に変換
<link rel="canonical" href="https://haik.oi21.net/仮想化">


存在するページ名は、

#canonical(Ajaxでテスト01);
が次の様になる
<link rel="canonical" href="https://haik.oi21.net/index.php?Ajax%E3%81%A7%E3%83%86%E3%82%B9%E3%83%8801">


URLを指定した場合、

#canonical(https://haik.oi21.net/index.php?Ajaxでテスト01);
は次の様になる
<link rel="canonical" href="https://haik.oi21.net/https://haik.oi21.net/index.php?Ajaxでテスト01">
https://haik.oi21.net/

の部分がダブる。変だ。

#canonical(https://haikforum.qhmtips.com/patio/);
は、
<link rel="canonical" href="https://haikforum.qhmtips.com/patio/">
となる。


URLを予めエンコードしておいて指定・・・URLをChromeのURLの枠からコピー、貼付けすると自動的にエンコードした状態で貼り付け可能。エンコード前は「https://haik.oi21.net/index.php?Ajaxでテスト01」 と表示されていた。

#canonical(https://haik.oi21.net/index.php?Ajax%E3%81%A7%E3%83%86%E3%82%B9%E3%83%8801);
は、
<link rel="canonical" href="https://haik.oi21.net/index.php?Ajax%E3%81%A7%E3%83%86%E3%82%B9%E3%83%8801">
となった。


おかしい部分を修正

2021年2月25日時点で

#canonical(https://haik.oi21.net/index.php?Ajaxでテスト01);

という感じで指定すると、 は次の様になる

<link rel="canonical" href="https://haik.oi21.net/https://haik.oi21.net/index.php?Ajaxでテスト01">

httpsの部分がダブる。明らかにおかしい。canonicalの表示の際に、URLが「https:」や「http:」で始まっていない時に、自サイトのURLのベース部分をURLの頭に付加してから表示するようになっているようです。これには、plugin/canonical.inc.php ファイルの

    // Relative Path
    else
    {
        $base_dir = dirname($script . 'dummy');
        $canonical_url = $base_dir . '/' . $target;
    }
 
    $qt->setv('canonical_url', $canonical_url);
 
    return plugin_canonical_show_info();
}
 
function plugin_canonical_show_info()

の部分を、以下の様のすると正常に動作するように思われます。

    // Relative Path
    else
    {
        $targetEncode = encodeURI($target);
        if (preg_match('/^http/',$target)) {
			$canonical_url = $targetEncode;
		} else {
			$base_dir = dirname($script . 'dummy');
			$canonical_url = $base_dir . '/' . $targetEncode;
		}
    }
 
    $qt->setv('canonical_url', $canonical_url);
 
    return plugin_canonical_show_info();
}
 
 
// https://stackoverflow.com/questions/4929584/encodeuri-in-php/6059053
function encodeURI($uri)
{
    return preg_replace_callback("{[^0-9a-z_.!~*'();,/?:@&=+$#-]}i", function ($m) {
        return sprintf('%%%02X', ord($m[0]));
    }, $uri);
}
 
 
function plugin_canonical_show_info()
{