An example of a canonical URL would be mysite.com/family/children/problems as opposed to mysite.com/problems3.html.
Canonical URLs are usually defined through a mix between Apache's .htaccess and a database of page name.
You should probably find an existing script for it as I'm sure it would take you months or years to figure it out.