josegonzalez/josegonzalez.github.io

How to prevent duplicate content?

Closed this issue · 10 comments

So as we know you can create a route to a controller action, but there is nothing stopping the site serving that same data out on the controller url.

For example

Router::connect('/deals', ['controller' => 'products', 'action' => 'deals']);

This will allow the same content to be accessed on both example.com/deals and also on example.com/products/deals.

What is a good strategy for dealing with this? Rewrites in the server config, which doesn't seem flexible enough? A custom dispatcher to check routes before serving any pages? Or a component which checks at the controller action level?

I would be interested in your thoughts.

Ta,
Dave

https://github.com/josegonzalez/cakephp-sham/blob/master/Controller/Component/ShamComponent.php#L240

I think you can set the autoRun option to true and use that. Don't quote me on it. If it doesn't work, comment and I'll fix it up.

Would this approach work from a more generalised perspective, such as a single component to manage the routing? Or would extending the Router to include this functionality be more beneficial?

If you wanted to do it in the Router, you could use a custom dispatcher using the beforeDispatch method that would essentially do the above.

I think I normally make such things a one-off in the actions themselves. It's especially hard to deal with this problem when you use named parameters - not that you should use them.

I would use a single component/dispatch filter to manage this, and only conditionally check, as doing this check over every action seems like it might be more overhead than you'd want.

I agree, but our SEO guy says it's the most important thing ever 🎢

Oh yeah I am well aware. We do the same thing, with a check within the controller for the correct route. You could/should do similar via the dispatch filter, and ignore anything that has a custom extension (like csv or pdf) since those are likely unnecessary for SEO.

Cool, thanks for the input dude, appreciated.

@davidyell I humbly request that you blog about any approach you take :)

If I get to it, I'll be sure to add it to http://jedistirfry.co.uk/

Lo and behold, this has been raised at work. So I'll be sure to post up my approach.

Of course it has :)