开发者

What's the best way to 301 redirect pages in CakePHP?

I have rewritten my site in Cakephp and choosen to keep the new Cakephp structure. I wa开发者_StackOverflows wondering if I could use routing in Cakephp for 301-routing (permanently moved).

I want to redirect resources.php, languages.php, clips.php, possibly *.php, to /resources/, /languages/, /clips.

Can this type of 301 redirecting be easily done in CakePHP? I could even write a simple admin-interface to add 301-links, e.g. from a MySQL table to easily administer redirects. Or is it better to do this manually via mod_rewrite?


I'm not sure about the best way, but I would first put routing at routes php like:

Router::connect('/resources.php', array(
    'controller' => 'resources', 
    'action' => 'index'
    )
);

(and so on)

After that check at start of the action function which route was used, and if *.php route was used do a 301 redirect:

$this->redirect(array('controller' => 'resources', 'action' => 'index'), 301);

I guess there is also "smarter" way to implement this but this was the idea. (use of before_filter etc)


Since CakePhp 2.x there is Router::redirect() method.

So you could add redirection in your routs:

Router::redirect(
    '/resources.php', 
    array(
        'controller' => 'resources', 
        'action' => 'index'
    ), 
    array('status' => 301)
);

The third parameter array('status'=>301) is not necessary because 301-redirect is used by default.

See Redirect routing — CakePHP Cookbook v2.x documentation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜