shihabudheen
shihabudheen

Reputation: 696

Rewrite URL if controller is not existing in codeigniter

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

Answers (3)

shihabudheen
shihabudheen

Reputation: 696

  1. Add routes to all existing controllers under "/project/..."
  2. Add a route that will match any paths under "/project"

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

Ryan McDonough
Ryan McDonough

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

Deadlock
Deadlock

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

Related Questions