2009/08/31

GOOGLEのCanonical URL

動的URLだと、ひとつの商品に付き複数のURLが発生します。
googleなどの検索エンジンのリスティングではNGらしいので
こういった場合の対策として
<link rel="canonical" href="登録したいURL" />

ってのを挟めば良いそうです。

なのでoscommerceに組み込んでみます。

../catalog/includes/function/general.phpの最後の行に

//Canonical URL対策はじまり
function CanonicalUrl() {
$domain = substr((($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER), 0, -1); // gets the base URL minus the trailing slash
$string = $_SERVER['REQUEST_URI']; // gets the url
$search = '\&osCsid.*|\?osCsid.*'; // searches for the session id in the url
$replace = ''; // replaces with nothing i.e. deletes
echo $domain . ereg_replace( $search, $replace, $string ); // merges the variables and echoing them
}
//Canonical URL対策おしまい
ってなくだりを入れて

動的URLになるページのヘッダーに(index.php,default.php,product_info.php)
これを挟み込む。
<link rel="canonical" href="<?php CanonicalUrl(); ?>" />