CakePHP: Redirect url in routes.php - php

I'm not sure what I'm doing wrong but I'm trying to redirect an old url to a new url using routes.php
old url = www.mywebsite.com/old_url
new url = www.mywebsite.com/sub_folder/old_url
here's all the different attempts I've tried independently and all have failed:
Router::connect('/old_url', array('controller' => 'my_controller', 'action' => 'old_url'), array('pass' => array('/sub_folder/old_url')));
Router::connect('/old_url', array('controller' => 'my_controller', 'action' => 'old_url'));
Router::redirect('/old_url', array('controller' => 'my_controller', 'action' => 'old_url'));
Router::redirect('/old_url', array('controller' => 'my_controller', 'action' => 'old_url'), array( 'pass' => '/sub_folder/old_url'));
Router::redirect('/old_url', array('controller' => 'my_controller', 'action' => 'old_url'), array( 'persist' => '/sub_folder/old_url'));
I've looked through the book. Is this possible? Any guidance is appreciated.

Router::connect('/old_url/*', array('controller' => 'my_controller', 'action' => 'old_url'));

Related

Make Cakephp urls more SEO friendly

I know this question is been asked several times, and I went through all other solutions, but I could do not solve the issue.
https://www.example.com/cars/view/161/BMW-X5
to be
https://www.example.com/cars/161/BMW-X5
my routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/admin',array('controller' => 'users', 'action' => 'login', 'admin' => true));
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';
You can simply connect with
Router::connect('/cars/*', array('controller' => 'cars', 'action' => 'view'));
CarsController::view() will receive single argument, which should be used to find car by name.
Link with $this->Html->link('My Link', array('controller' => 'cars', 'action' => 'view', $slug));
For more route components, you need to use named parameters:
Router::connect('/cars/:id/:slug',
['controller' => 'cars', 'action' => 'view'],
[
// :id parameter will be passed as argument of CarsController::view()
'pass' => ['id']
]
);
Link with $this->Html->link('My Link', array('controller' => 'cars', 'action' => 'view', 'id' => $id, 'slug' => $slug));

301 Redirect CakePHP

I've recently started a new job and I noticed that a page linked to in one of our mailing lists no longer exists. I'm a developer / sysadmin so I thought it would be simple to set up a redirect, however our site is running CakePHP on the backend which I've got no experience with.
I want to redirect oursite.com/page/online-store to just oursite.com
I've tried Router::redirect('/page/online-store', 'http://www.oursite.com', array('status' => 301)); at the bottom of that file but it doesn't seem to be working, it just hangs at page load until I comment that line out.
Relevant lines in routes.php:
// login and register
Router::connect('/login', array('controller' => 'accounts', 'action' => 'login'));
Router::connect('/register', array('controller' => 'accounts', 'action' => 'register'));
// home
Router::connect('/', array('controller' => 'site', 'action' => 'home'));
Router::connect('/admin', array('controller' => 'users', 'action' => 'login', 'admin' => true));
// dashboard
Router::connect('/admin/dashboard', array('controller' => 'pages', 'action' => 'dashboard', 'admin' => true));
// search
Router::connect('/search', array('controller' => 'products', 'action' => 'search'));
// pages
Router::connect('/page/site-map', array('controller' => 'site', 'action' => 'map'));
Router::connect('/page/home', array('controller' => 'site', 'action' => 'home'));
Router::connect('/page/*', array('controller' => 'pages', 'action' => 'index'));
I'd appreciate any help.

CakePHP routing not work with switching language?

I have :
Router::connect('/:language/:controller/:action/*', array(), array('language' => 'en|zh'));
Router::connect('/:language/:controller', array('action' => 'index'), array('language' => 'en|zh'));
Router::connect('/:language', array('controller' => 'welcome', 'action' => 'index'), array('language' => 'en|zh'));
I want to add one more route like below:
Router::connect('/profile', array('controller' => 'userProfile', 'action' => 'index'));
When I go to : www.xxxxx.com/profile , it work as normal but when I go to www.xxxxx.com/en/profile , I receive an error is missing controller.
How can I do to go to the www.xxxxx.com/en/profile without got any errors?
Please help! Thank you in advanced for any helps!
You just need to declare that route line two time.
Router::connect('/profile', array('controller' => 'userProfile', 'action' => 'index'));
Router::connect('/:language/profile', array('controller' => 'userProfile', 'action' => 'index'), array('language' => 'en|zh'));
Use that way and let me know your thoughts regarding for the same.
Thanks
You can use
Router::connect('/:language/profile', array('controller' => 'userProfile', 'action' => 'index'), array('language' => 'en|zh'));

301 Redirects with Cakephp

I am trying to 301 redirect a url to a new place since the original pages don't exist anymore. The URL I am trying to run is Guns/Rifles/Heckler_Koch/MP5_A4. I need this to redirect to Guns/Heckler_Koch/. I have tried so many things, but this is the only thing I could come up with and it doesn't work. When I run this it brings me to, manufacturers/items/. Thank you for your help.
Router::connect(
'/Guns/:manufacturer',
array('controller' => 'manufacturers', 'action' => 'items'),
array('pass' => array('manufacturer'), 'routeClass' => 'GunRoute')
);
Router::redirect(
'/Guns/Rifles/:manufacturer/*',
array('controller' => 'manufacturers', 'action' => 'items'),
array('pass' => array('manufacturer'), 'status' => '301')
);
Router::connect(
'/Guns/:manufacturer',
array('controller' => 'manufacturers', 'action' => 'items'),
array('persist' => array('manufacturer'), 'routeClass' => 'GunRoute')
);

Cakephp Nested Routes

I've got a controller called Items and another one called Categories. Items may belong to different Categories.
The routes that I have are
/items
/items/:item_id
/items/categories
/items/categories/:category_id
And this is my routes.php
Router::connect('/items', array('controller' => 'items', 'action' => 'index');
Router::connect('/items/:item_id', array('controller' => 'items', 'action' => 'view'), array('pass' => array('item_id')), array("item_id" => "[0-9]+"));
Router::connect('/items/categories', array('controller' => 'categories', 'action' => 'index'));
Router::connect('/items/categories/:category_id', array('controller' => 'categories', 'action' => 'view', array('pass' => array('category_id')), array("category_id" => "[0-9]+"));
The problem is that the third route fails as it routes to the Items controller which it shouldn't.
How can I fix this problem?
be careful on the order of your routes... cake will match to the first occurrence, so try using your routes like this:
Router::connect('/items', array('controller' => 'items', 'action' => 'index');
Router::connect('/items/categories', array('controller' => 'categories', 'action' => 'index'));
Router::connect('/items/categories/:category_id', array('controller' => 'categories', 'action' => 'view', array('pass' => array('category_id')), array("category_id" => "[0-9]+"));
Router::connect('/items/:item_id', array('controller' => 'items', 'action' => 'view'), array('pass' => array('item_id')), array("item_id" => "[0-9]+"));
good luck
in you config/routes.php
put a line
Router::connect('/items/categories', array('controller' => 'categories',
'action' => 'index'));
or something similar.
it will redirect all you /items/categories to the index action of categories controller.
for admin pages (actions that use admin prefix):
Router::connect('/items/categories', array('controller' => 'categories',
'action' => 'index','admin'=>true));

Categories