Downloads

Google Ad

CakePHP Search Engine Optimization Plugin

Date: Mon, Jan 3rd 2011, 13:12 Author: nick Views: 22930 Comments share

CakePHP SEO Plugin

Info:
Watch:
Get it:
  • Download Now
  • git clone git://github.com/webtechnick/CakePHP-Seo-Plugin Seo

The Goal

I've been working on a few tools to help my good friend Doug Lay, an SEO expert, to optimize a CakePHP based application for search engines. I had two goals when developing this plugin

1) An Easy, yet powerful, 301 redirecting tool complete with regular expression support and model callbacks to find correct slugs uris.

2) Dynamic Meta Tags customizable site wide on a global or micro scale, complete with regular expression support.

With these two goals in mind I developed the Search Engine Optimization plugin for CakePHP. A drop-in solution for most of what haunts the SEO Expert's nightmares.

Install and Setup


To install the plugin you'll need to clone/download the plugin into your app/plugins/seo directory and then run the schema into your database.
  1. cake schema create seo -plugin seo

Create the file app/config/seo.php with the following configurations like so:
  1. $config = array(
  2.     'Seo' => array(
  3.         'approverEmail' => 'nick@example.com',
  4.         'replyEmail' => 'noreply@example.com',
  5.         'parentDomain' => 'http://www.example.com'
  6.     )
  7. );

SEO Redirect Quick Start Guide

1) Create the file app/app_error.php with the following

  1. App::import('Lib','Seo.SeoUtil');
  2. SeoUtil::loadSeoError();
  3. class AppError extends SeoAppError {
  4. }

2) Then navigate to /admin/seo/seo_redirects to start adding 301 redirects.

SEO Meta Tags Quick Start Guide

1) Include the Seo.Seo helper in your app/app_controller.php file.

  1. var $helpers = array('Seo.Seo');

2) Alter your layout to include the SEO Meta Tags in the head.

[geshi=html] Seo->metaTags(); ?>
3) Navigate to /admin/seo/seo_meta_tags to start adding Meta Tag data to your site

Advanced Callback Feature

For all what mod_rewrite cannot do for you, SEO plugin can!

By defining a callback in the SEO Redirect you can do some very powerful things, most notably the ablity to do what .htaccess redirect can't, access your database to find the correct url instead of showing a 404.

A good example of this would be redirecting broken article urls with a id-slug combination. With a callback no matter what the slug, you can 301 redirect to the correct id-slug combination with one rule.

Callback Senorio

We have an article URL of http://www.example.com/article/123-slug where 123 is the id, and slug is the slug. What we'd like to do is 301 redirect any broken link with the id 123 to the correct URL with the correct slug. We could use wild card rules for every article, or we can just write one rule with a callback to Article. Here's how:

URI: #/article/([0-9]*)(.*)#i
Redirect: /article/$1-{callback} Callback: Article::callbackFindSlugById
  1. //app/models/article.php
  2.     function callbackFindSlugById($request){
  3.       $string = preg_replace("/[^0-9\-]/","", $request);
  4.       $id = array_shift(explode("-",$string));
  5.       if($id){
  6.         $this->id = $id;
  7.         return $this->field('slug');
  8.       }
  9.       else {
  10.         return false;
  11.       }
  12.     }

Note: If the callback returns false, the Redirect will be haulted.

This callback redirect rule will redirect any broken article link to the correct `id-slug` syntax.


Wiki Links

For more information, and complete documentation please visit the repository wiki on github.com

Working with Seo Redirects - https://github.com/webtechnick/CakePHP-Seo-Plugin/wiki/Seo-Redirects
Working with Seo Meta Tags - https://github.com/webtechnick/CakePHP-Seo-Plugin/wiki/Seo-Meta-Tags

Enjoy

As always, any comments/feedback is greatly appreciated. I hope you enjoy the work I put into this plugin.