Zend Framework Logo.I got a simple way to do canonical urls for a web application that uses Zend Framework (one of the best tools to making building a website fast and easy) for a smaller website. This might not be the best way to do it, but it sure works.

You want to do canonical urls so that search engines like Google will know exactly what url you want to be indexed.

For example, do you want your website to use http://www.example.com/index.html, or do you want http://example.com, or http://example.com/index.html, etc? This way if someone links to you in one way, it will always revert back to the url you specified in your header tag.

So, in your Zend Framework application you will want to go to your application folder depending on how you have your file structure set up. Then your layout folder, then your scripts folder, then open up to edit your layout.phtml file.

Now within the <head> tag, put this code and replace with how many pages you have on your site and how you want the urls to look like.

<link rel="canonical" href="<?php $request = Zend_Controller_Front::getInstance()-> getRequest()->getActionName(); if ($request == 'index') echo 'http://example.com'; elseif ($request == 'contact') echo 'http://example.com/contact'; elseif ($request == 'about') echo 'http://example.com/about'; elseif ($request == 'services') echo 'http://example.com/services'; else echo ''; ?>" />

So the $request variable simply gets the instance of the request from your controller, so there is nothing else needed to do to make this code work in your Zend Framework project. Hope this info is useful to you.

Tags: , , , , , , , , , ,

2 Comments to “One easy way to do dynamic canonical urls using Zend Framework on a small website”

  1. Lewis says:

    Is the Zend framework really a good choice? I see that you’re using WP for your site.


    mywebmastertips Reply:

    WordPress is great for blogging and content management. Zend Framework is great when you need to build a unique site that has unique functionality like an affiliate program or user account subscription service etc…


Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>