0

I want to redirect www.myhost.com/g/:id to www.myhost.com/jobs/view/:id. Currently the following code does that fine:

$routes->connect(
    '/g/:id',
    ['controller' => 'Jobs', 'action' => 'view'],
    ['pass' => ['id'], 'status' => 301, 'persist' => ['id']]
);

However, in the addressbar it still shows www.myhost.com/g/:id. I'd like it to show the full URL (www.myhost.com/jobs/view/:id), not the 'shortened' URL.

How would I adjust the code to do the above?

Bird87 ZA
  • 2,313
  • 8
  • 36
  • 69

1 Answers1

1

Router::connect is a way of connecting one route to another, without a full redirect.

If you're wanting redirect a user and have that exposed to them, you can use Router::redirect(). It automatically will set the 301 status.

$routes->redirect(
    '/g/:id', 
    ['controller' => 'Jobs', 'action' => 'view'], 
    ['pass' => ['id'], 'persist' => true]
);
Trenton Trama
  • 4,890
  • 1
  • 22
  • 27
  • Reading your answer made me realise that I could try [`$routes->redirect`](https://book.cakephp.org/3.0/en/development/routing.html#redirect-routing) which solved my problem. Now it's just adding a silly `?id=:id` at the end. So it's turning `http://www.myhost.com/g/:id` into `http://www.myhost.com/jobs/view/:id?id=:id` where `:id` is the actual integer value being thrown around. If you update your answer, I'll add upvote and accept. – Bird87 ZA Jun 25 '18 at 15:54
  • Can you just update the question so `redirect`'s arguments look like this: `'/g/:id', ['controller' => 'Jobs', 'action' => 'view'], ['pass' => ['id'], 'persist' => true]` because this is the exact code that worked – Bird87 ZA Jun 26 '18 at 13:04