非ログインユーザーへの SEO 対策、「site_post.php」で強制的に cID パスを正規パスに

2011年7月31日 at 6:03

アンドリューの記事
http://andrewembler.com/posts/seo-tip-force-concrete5-pages-to-display-at-one-url/


/config/site_post.php
に空のテキストファイルを作り、以下のコードをそのままコピペ

<?php
$req = Request::get();
if ($req->getRequestCollectionID() > 1 && $req->getRequestPath() == ''
&& $_SERVER['REQUEST_METHOD'] != 'POST') {
// This is a request that is directly for the cID, rather than the path
$u = new User();
// If the user is logged in we do NOT redirect
if (!$u->isRegistered()) {
// Get the page object for the current cID
$c = Page::getByID($req->getRequestCollectionID());
if (!$c->isError()) {
$nav = Loader::helper('navigation');
header ('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $nav->getLinkToCollection($c, true));
exit;
}
}
}



これにより、非ログインユーザーは

http://EXAMPLE.COM/index.php?cID=XXX

というアドレスでアクセスしても

http://EXAMPLE.COM/page-path/

という正規のアドレスに強制的にリダイレクトされます。

自分のサイトが、 Twitter や Facebook で共有される時に、「/index.php?cID=XXX」で共有されてしまうと、URLベースでカウントしている Twitter や Facebook のいいねの数がきちんとカウントされなくなるのを防ぐ為です。