Reputation: 696
Rewrite URL if user tried to access any non existing controller.
Ex:- If user tried to access http://example.com/project/anyvalue
. In my program there is no controller with name 'anyvalue'. In this situation I want to redirect to
http://example.com/project/profile/anyvalue
How is this possible using routing in codeigniter?
Upvotes: 1
Views: 2013
Reputation: 696
Example:
/* Currently available controllers under "/project/" */
$route['project/profile'] = "project/profile";
$route['project/add'] = "project/add";
$route['project/edit'] = "project/edit";
/* Catch all others under "/project/" */
$route['project/(:any)'] = "project/profile/$1";
/* if controller class name is Profile and function name is index */
$route['project/(:any)'] = 'project/profile/index/$1';
Upvotes: 1
Reputation: 10012
What you want is Vanity URLs, you can find a guide for performing this in code igniter here:
http://philpalmieri.com/2010/04/personalized-user-vanity-urls-in-codeigniter/
Essentially you're adding this to your routes file:
$handle = opendir(APPPATH."/modules");
while (false !== ($file = readdir($handle))) {
if(is_dir(APPPATH."/modules/".$file)){
$route[$file] = $file;
$route[$file."/(.*)"] = $file."/$1";
}
}
/*Your custom routes here*/
/*Wrap up, anything that isnt accounted for pushes to the alias check*/
$route['([a-z\-_\/]+)'] = "aliases/check/$1";
Upvotes: 0
Reputation: 1577
Use default route to redirect requests to some particular page if controller is missing
You can find routes location in
/application/admin/config/routes.php
$route['default_controller'] = "welcome";
Also use following in case of page not found
$route['404_override'] = 'default_page';
Upvotes: 1