Redirect route cakephp

sorry, my English so bad.

In CakePHP 2.x, I want to redirect old link to new link in my website. But i got some issue.

Example:

mywebsite.com/news/news redirect to mywebsite.com/news I use:

Router::redirect ('/news/news',
        array('controller' => 'categories', 'action' => 'index', 'slug' => 'news'),
        array('status' => '301')
    );
Router::connect('/news', array('controller' => 'categories', 'action' => 'index', 'slug' => 'news'));

=> OK, it work.

But

Example: mywebsite.com/news/news/a-b-c redirect to mywebsite.com/news/a-b-c

I use:

Router::redirect ('/news/events/:slug',
        array('controller' => 'articles', 'action' => 'view', 'category' => 'events'),
        array('status' => '301')
    );

Router::connect('/news/:slug',
            array('controller' => 'articles', 'action' => 'view', 'category' => 'news')
        );

=> result is: mywebsite.com/articles/view/category:news

Please help, thank you !

It sounds like you have a controller called news and you want the view action to be implied.

So: /news/view/article-a-b-c --> /news/article-a-b-c

I have had similar setups. If I have no other action I want to use for that controller, I simply reroute /news/* and call it a day.

If I have other actions I want to use, I choose one of two possible approaches:

  1. create an alias for the controller + view action pairing (say, ‘newsline’) then reroute /newsline/* to /news/view/

  2. reroute /news/* to /news/view/ regardless… then, setup exceptions inside the view controller:
    over-simplistic example:

    function view($uname,$arg=null) {
    if($uname==‘edit’) $this->edit($arg);
    }

it’ true. But more old link on my website like this ( mywebsite.com/news/news/article-alias ), so i want to redirect them to new link like this ( mywebsite.com/news/article-alias ).
But it’s not work with my code in routes.php:

Router::redirect ('/news/news/:slug',
        array('controller' => 'articles', 'action' => 'view', 'category' => 'news'),
        array('status' => '301')
    );

Router::connect('/news/:slug',
            array('controller' => 'articles', 'action' => 'view', 'category' => 'news')
        );

Sorry, i have some mistake in my question :slight_smile:

It’s not the most elegant solution, but you could create a news action inside your news controller that hands off the arguments to the action you actually want to use. At least that way your urls would continue to work correctly.

It’s good idea, but it’s not good for my website. Because my website have 2 type link mywebsite.com/news/article-alias and mywebsite.com/news/news/article-alias working.

So i think i use: http://book.cakephp.org/2.0/en/development/routing.html#redirect-routing