I need to redirect (301) urls like this:
https://website.com/products/view/id
to:
https://website.com/products/product-name-slug
I know how to make the slug version of urls work, but I do not want to disable old ones completely, instead I want them to 301-redirect to its slug version.
I don’t want them both working simultaneously as it is treated as duplicated content by Google SEO.
I also do not want to write redirect for every product in routes.php like this:
Router::redirect( '/product/view/1001', [ 'controller' => 'products', 'action' => 'view', 'product-1001-name-slug' ] );
Instead I would rather have dynamic lookup function to do it for me.
How can I do it in CakePHP 2.x?
2
Answers
This is an example implementation of @burzum's answer:
in routes.php:
in app/Routing/Route/IdToSlugRoute.php:
in app/Controller/BaseProductsController.php:
additionality: