Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 was 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?

like image 727
Dennis Avatar asked Aug 21 '11 11:08

Dennis


1 Answers

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)

like image 122
Henri Avatar answered Sep 26 '22 16:09

Henri