# Redirect route cakephp

**URL:** <https://discourse.cakephp.org/t/redirect-route-cakephp/763>\
**Category:** Need Help\
**Created:** [July 20, 2016, 4:04am UTC](https://discourse.cakephp.org/t/redirect-route-cakephp/763 "2016-07-20T04:04:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![quanganhcnt50](https://avatars.discourse-cdn.com/v4/letter/q/898d66/32.png) [@quanganhcnt50](https://discourse.cakephp.org/u/quanganhcnt50)\
**Post date:** [July 20, 2016, 4:04am UTC](https://discourse.cakephp.org/t/redirect-route-cakephp/763/1 "2016-07-20T04:04:38Z")

</div>

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](http://mywebsite.com/news/news) redirect to [mywebsite.com/news](http://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](http://mywebsite.com/news/news/a-b-c) redirect to [mywebsite.com/news/a-b-c](http://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](http://mywebsite.com/articles/view/category:news)

Please help, thank you !

---

<div class="post-metadata">

**Author:** ![calzone](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/calzone/32/305_2.png) [@calzone](https://discourse.cakephp.org/u/calzone)\
**Post date:** [July 20, 2016, 4:16am UTC](https://discourse.cakephp.org/t/redirect-route-cakephp/763/2 "2016-07-20T04:16:10Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![quanganhcnt50](https://avatars.discourse-cdn.com/v4/letter/q/898d66/32.png) [@quanganhcnt50](https://discourse.cakephp.org/u/quanganhcnt50)\
**Post date:** [July 20, 2016, 4:23am UTC](https://discourse.cakephp.org/t/redirect-route-cakephp/763/3 "2016-07-20T04:23:11Z")

</div>

it’ true. But more old link on my website like this ( [mywebsite.com/news/news/article-alias](http://mywebsite.com/news/news/article-alias) ), so i want to redirect them to new link like this ( [mywebsite.com/news/article-alias](http://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 🙂

---

<div class="post-metadata">

**Author:** ![calzone](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/calzone/32/305_2.png) [@calzone](https://discourse.cakephp.org/u/calzone)\
**Post date:** [July 20, 2016, 4:29am UTC](https://discourse.cakephp.org/t/redirect-route-cakephp/763/4 "2016-07-20T04:29:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quanganhcnt50](https://avatars.discourse-cdn.com/v4/letter/q/898d66/32.png) [@quanganhcnt50](https://discourse.cakephp.org/u/quanganhcnt50)\
**Post date:** [July 20, 2016, 4:37am UTC](https://discourse.cakephp.org/t/redirect-route-cakephp/763/5 "2016-07-20T04:37:23Z")

</div>

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

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