SEO ドキュメント
pieceofcake2/seo は、CakePHP 2 アプリケーションの検索エンジン最適化 (SEO) のためのツールキットで、 管理画面から管理します。
- 404 になる場合にだけ読み込まれる、手軽で強力な 301 リダイレクトツール
- レーベンシュタイン距離と
sitemap.xmlを使う、細かく設定できる賢い 404 の URL 推測 - 任意のリクエスト URI に対して、細かく設定・カスタマイズできる メタタグ
- URI に応じた タイトルタグ の上書き
- URI に応じた canonical リンク
- URI に応じた独自の ステータスコード
- スクレイパーが自ら禁止対象になるように誘い込むハニーポットまで備えた スクレイパー禁止 の管理
- URI に応じた Google Analytics の A/B テスト の管理
バージョン履歴は CHANGELOG.md をご覧ください。
インストール
Composer でプラグインをインストールします。
composer require pieceofcake2/seoこのプラグインのタイプは cakephp-plugin なので、composer/installers によってプラグインディレクトリに インストールされます。正しい場所にインストールされるように、アプリケーションの composer.json で installer-paths を設定してください。
| レイアウト | installer-paths |
|---|---|
モダン (config/、src/、templates/、plugins/) | "plugins/{$name}/": ["type:cakephp-plugin"] |
レガシー (app/Config/、app/Plugin/) | "app/Plugin/{$name}/": ["type:cakephp-plugin"] |
config/bootstrap.php (レガシーレイアウトでは app/Config/bootstrap.php) でプラグインを読み込みます。
CakePlugin::load('Seo');データベースにプラグインのテーブルを作成します。
bin/cake schema create --plugin Seo
# legacy layout
app/Console/cake schema create --plugin SeoSQL は Config/sql/install.sql にもあります。
設定
プラグインの Config/seo.php.default を config/seo.php (レガシーレイアウトでは app/Config/seo.php) にコピーして、調整します。
$config = [
'Seo' => [
'approverEmail' => 'nick@example.com',
'replyEmail' => 'noreply@example.com',
'emailConfig' => 'default', // config of your email, if false will use default CakeEmail()
'parentDomain' => 'http://www.example.com',
'aggressive' => true, // if false, log offenders for later review instead of autobanning
'triggerCount' => 2,
'timeBetweenTriggers' => 60 * 60 * 24, // seconds
'honeyPot' => ['admin' => false, 'plugin' => 'seo', 'controller' => 'seo_blacklists', 'action' => 'honeypot'],
'log' => true,
'cacheEngine' => false, // optionally cache things to save on DB requests - eg: 'default'
'searchTerms' => true, // turn on term finding
'levenshtein' => [
'active' => false,
'threshold' => 5, // -1 to ALWAYS find the closest match
'cost_add' => 1, // cost to add a character, higher the amount the less you can add to find a match
'cost_change' => 1, // cost to change a character, higher the amount the less you can change to find a match
'cost_delete' => 1, // cost to delete a character, higher the amount the less you can delete to find a match
'source' => '/sitemap.xml', // URL to list of urls in a sitemap
],
'abTesting' => [
'category' => 'ABTest', // Category for your ABTesting in Google Analytics
'scope' => 3, // Scope for your ABTesting in Google Analytics
'slot' => 4, // Slot for your ABTesting in Google Analytics
'legacy' => false, // Uses Legacy version of Google Analytics JS code pageTracker._setCustomVar(...)
'session' => true, // will use sessions to store tests for users who've already seen them.
'redmine' => false, // or the full URL if your redmine http://www.redmine-example.com/issues/
],
'dnsPrefetch' => [
// Default prefetches, starting with // or http:// or https://
// '//fonts.googleapis.com',
],
],
];このファイルは、Seo の設定がまだ構成されていなければ、設定が初めて読み込まれるときに Configure::load('seo') で読み込まれます。
管理画面
リダイレクト、ステータスコード、メタタグ、タイトル、canonical リンク、ブラックリスト、A/B テストは /admin/seo/... で管理します。config/core.php (レガシーレイアウトでは app/Config/core.php) で admin ルーティングプレフィックスを有効にし、管理画面をほかの管理エリアと同じように保護してください (プラグインのコントローラーはアプリケーションの AppController を継承します)。
Configure::write('Routing.prefixes', ['admin']);リダイレクト、ステータスコード、メタタグ、タイトル、canonical リンク、A/B テストは URI (seo_uris テーブル) に結び付けられ、URI は https://example.com/admin/seo/seo_uris で管理します。 URI は、新しい URI でこれらのいずれかを保存したときに作成されます。正規表現の URI は、使われる前に そこで承認する必要があります (高度なリダイレクト をご覧ください)。
canonical リンク
AppController.php で Seo.Seo ヘルパーを読み込みます。
public $helpers = ['Seo.Seo'];レイアウトを変更して、レイアウトの head に SEO の canonical リンクを含めます。
<head>
<!-- other head items -->
<?= $this->Seo->canonical() ?>
</head>canonical リンクの追加
https://example.com/admin/seo/seo_canonicals
DNS プリフェッチ
$this->Seo->dnsPrefetch() は、dnsPrefetch 設定のホストと引数で渡したホストについて <link rel="dns-prefetch"> タグを出力します。
<head>
<?= $this->Seo->dnsPrefetch(['//maps.googleapis.com']) ?>
</head>ブラックリスト
AppController.php で Seo.BlackList コンポーネントを読み込みます。
public $components = ['Seo.BlackList'];悪意のあるコンテンツ収集ツールをおびき寄せるために、アプリケーションの中やその周辺にハニーポットのリンクを 追加していきます。
<?= $this->Seo->honeyPot() ?>robots.txt を更新して、/seo/ をクロールの対象から外します。正当なクローラーはすべてハニーポットを 無視します。
User-agent: *
Disallow: /seo/禁止 IP の追加・管理
https://example.com/admin/seo/seo_blacklists
A/B テスト
AppController.php で Seo.Seo ヘルパーと Seo.ABTest コンポーネントを読み込みます。
public $helpers = ['Seo.Seo'];
public $components = ['Seo.ABTest'];サイトの Google Analytics のコードに、次のように行を追加します。
<script type="text/javascript">
<!-- GA Items -->
var pageTracker = _gat._getTracker('UA-SOMEKEY');
<?= $this->Seo->getABTestJS() ?>
</script>生成されるコードは、従来の Google Analytics トラッキングコードのカスタム変数 (_gaq.push(['_setCustomVar', ...])、abTesting.legacy の場合は pageTracker._setCustomVar(...)) を使います。
テスト対象のページかどうかを確かめてそのページを表示するには、AppController.php で 次のようにします。
/**
* Serve the view of the A/B test of this page, if there is one
*
* @return void
*/
public function beforeFilter()
{
$test = $this->ABTest->getTest();
if ($test) {
// Do things specific to this test
$this->set('ABTest', $test);
$this->view = $test['SeoABTest']['slug'];
}
parent::beforeFilter();
}ヒント: GA で公開する前にコントローラーでデバッグするには、debug オプションを true にします。 まだ有効になっていないテストも返されます。
$test = $this->ABTest->getTest(['debug' => true]);
$this->set('ABTest', $test);A/B テストの追加
https://example.com/admin/seo/seo_a_b_tests
ヒント: A/B テストを debug に設定すると、コントローラーでは true が返されますが、GA のコードは トラッキングされません。
由来
このパッケージは webtechnick/CakePHP-Seo-Plugin (Packagist では webtechnick/cakephp-seo-plugin、アップストリームの最終バージョンは v6.1.0) のメンテナンスされたフォークで、元は Nick Baker、Alan Blount、Matt Reishus によって作られました。 pieceofcake2 が、モダンな PHP 上の CakePHP 2 向けにメンテナンスしています。 オリジナルからの変更点は CHANGELOG.md にあります。リダイレクトとステータスコード、メタタグ、 タイトル の各ページは、オリジナルのプラグインの wiki をもとにしています。
