I’m trying to implement canonical URLs and combine it with custom route-classes.
The URL-scheme is something like this:
/category-x/article/123
/category-y/article/123
I create a custom route-class extending Zend_Controller_Router_Route_Regex and checks that the article 123 exists and that the URL includes the correct category-name. If article 123 belongs in category-x and the user is accessing category-y I want to redirect to the correct URL.
But the routes does not have any obvious possibility to do this directly. What’s the best practice approach here?
I ended up with this solution:
The custom route-class creates the canonical URL in its match()-method like this:
$article is populated inside match() if the parent::match() returns array.
I’ve created a front controller plugin which hooks on the routeShutdown() like this:
It simply checks if the route(custom or native Zend) created a canonical URL and if the requested URL does not match, redirect to the correct canonical URL.