What is the best way to 301 page redirects in CakePHP?

I rewrote my site in Cakephp and decided to keep the new Cakephp structure. I was wondering if I can use routing in Cakephp for 301 routing (constantly moving).

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

Is it possible to easily redirect this type of 301 redirect to CakePHP? I could even write a simple admin interface to add 301 links, for example. from a MySQL table to easily administer redirects. Or is it better to do it manually through mod_rewrite?

+4
source share
2 answers

I am not sure of the best form, but first I would set up routing on php routes, for example:

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

(etc.)

After this check, when starting the action function that used the route, and if the * .php route was used, perform 301 redirects:

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

I think there is a more β€œreasonable” way to implement this, but it was an idea. (using before_filter etc.)

+8
source

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

So you can add a redirect to your routes:

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

The third parameter, array('status'=>301) not required since 301-redirect is used by default.

See Routing Redirection - CakePHP Cookbook v2.x Documentation .

+1
source

All Articles