maandag 6 oktober 2008

SEO your CakePHP application

I am Search Engine Optimizing an older CakePHP application. When I started, I was looking for a solution in .htaccess, but that's not the right way.

Your .htaccess is already set up to have CakePHP process all incoming URLs. It already knows what to do with your controllers and actions. The magic word is routing. Routing is your friend when you want to optimize your URLs.

Of course, when you develop a new CakePHP app, be sure to think about SEO and how your URLs will look like. If your app is finished, and your URLs don't seem very friendly use routing.

For example, if your current url is http://www.example.com/games/view/A56789 and you want to use http://www.example.com/games/platform/prince-of-persia.html, you could solve it like this:

Your controller is the games_controller, the function is view().

Add this to routes.php:

$Route->connect('/games/*', array('controller' => 'games', 'action' => 'view'));

So, when you call http://www.example.com/games/platform/prince-of-persia.html, the function view within games_controller will be called with two parameters, the first one is platform, the second is prince-of-persia.

You have to rewrite your function to show the right data. For example forget the first parameter, and use the second to identify the record in your database.

Don't forget to build your sitemap, maybe that will be the next article :)

-- Guillaume was digging in the source code of CakePHP and found: http://api.cakephp.org/inflector_8php-source.html#l00431

5 opmerkingen:

  1. Hello there,
    for what it's worth, I did a post on the subject some months ago: http://cherryonthe.popnews.com/2008/03/15/basic-seo-and-cakephp-part-1/

    BeantwoordenVerwijderen
  2. Hi Guillaume, thanks for your reply. The sluggable behavior doesn't fit in my situation, but I guess many others can use it.

    BeantwoordenVerwijderen
  3. Thanks Steven, so how do you generate your pretty urls? do you use the built-in method to get rid of space, ponctuation,etc. ?

    BeantwoordenVerwijderen
  4. I found some statements on the internet, which I used, and extended this with a few own statements. A few preg_replaces, str_replaces, a strtolower and at last, I append .html to the url.

    BeantwoordenVerwijderen
  5. Nice, I also use slug but this can be handy as well.

    Greetings

    BeantwoordenVerwijderen