开发者

CakePHP: Advanced 'SEO' Routing - HowTo save old URLs Routes for changed Posts, for 301 redirects of external links

i have a problem with the routing of a new project.

For most of the actions and entities like users, the URLs would never change, because user names i.e. are unique and not changable. For that stuff we can easily use Routes like these:

Router::connect('/a/:username/topic/:id', array('controller' => 'users', 'action' => 'view'),array('pass' => array('username','id'),'username' => '[^-]+','id' => '[0-9]+'));
Router::connect('/a/:username', array('controller' => 'users', 'action' => 'view'),array('pass' => array('username'),'username' => '[^-]+'));

But for posts (and some other entities) the titles could/will change due to typos or something else. Imagine you change the title of a post (or something else) 5 times. Each time some external links were build. So we want to assure that every external link still works and redirects to the current URL-Key with the correct/changed title with a 301 Status.

Because of that we tried to implement a table for routes with the following fields:

id, source, ref_id, target_controller, target_action, target_param, parent_id

Each time routes.php gets called, a foreach loop will run over the table and connect every route, that has NO parent. Having no parent means, that the record is the current (most recent) url key for a specific entity.

If the request goes onto an "old" url-key, we automatically redirect to the parent (most recent key) with a 301.

This does work pretty well but.... :

Later when this table contains thousands of records, the app will开发者_JAVA百科 read all those records (unless the cache is up to date) and will "router::connect" thousands of routes instead of 5-10 very generic routes like those seen above. And that every time our 'routes cache' is outdated (what is likely to be very often).

I ask myself if there is no other solution to keep all old routes running while no eating a lot of performance.

i'd be glad to receive any feedback. thanks a lot!


Rather than generate thousands of routes at run-time and caching them, have you considered using a custom Route class that simply works out the route for the current request?

Now in the past slug routes were a bit tricky as ensuring you had a valid slug either had to be done at the controller/model level, or you had to connect several hundred routes, one for each article slug. Both implementations leave much to be desired. ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜