Php 如果codeigniter中不存在控制器,请重写URL
如果用户试图访问任何不存在的控制器,请重写URL 示例:-如果用户试图访问Php 如果codeigniter中不存在控制器,请重写URL,php,codeigniter,url-rewriting,routing,Php,Codeigniter,Url Rewriting,Routing,如果用户试图访问任何不存在的控制器,请重写URL 示例:-如果用户试图访问http://example.com/project/anyvalue。在我的程序中,没有名为“anyvalue”的控制器。在这种情况下,我想重定向到 http://example.com/project/profile/anyvalue 如何使用codeigniter中的路由实现这一点?如果缺少控制器,请使用默认路由将请求重定向到某个特定页面 您可以在中找到路线位置 /application/admin/config/
http://example.com/project/anyvalue
。在我的程序中,没有名为“anyvalue”的控制器。在这种情况下,我想重定向到
http://example.com/project/profile/anyvalue
如何使用codeigniter中的路由实现这一点?如果缺少控制器,请使用默认路由将请求重定向到某个特定页面 您可以在中找到路线位置 /application/admin/config/routes.php
$route['default_controller'] = "welcome";
如果找不到页面,也请使用以下命令
$route['404_override'] = 'default_page';
您需要的是虚荣URL,您可以在下面的代码点火器中找到执行此操作的指南: 基本上,您要将其添加到路由文件中:
$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";
/* 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';
我很确定这只针对文档的根,而不是缺少的控制器。如果用户试图访问,那么我需要重定向到。这是项目简介页面。我不需要重定向到默认控制器。感谢您的支持。更新了ans以涵盖所有可能性。。谢谢你的意见@RyanMcDonoughTry在本用户指南中使用“URI路由”我可以像这样设置路由$route['project/^(?!other | controllers.*']='project/profile/$1';