如何从codeigniter中的URL中删除控制器名称?

时间:2013-11-05 10:33:33

标签: php codeigniter

我有一个名为“profile”的控制器,用户档案的URL是 www.example.com/profile/user

我已经在路由文件

中使用了codeigniter的重新路由
$route['profile/(:any)'] = "profile/index"; 

我正在寻找的是从URL中删除配置文件控制器名称,以便它是SEO和用户友好。

e.g

www.example.com/user

有什么建议吗?

4 个答案:

答案 0 :(得分:6)

您可以尝试其中任何一种

// url could be yourdomain/imran
$route['(:any)'] = 'profile/index/$1';
// url could be yourdomain/10
$route['(:num)'] = 'profile/index/$1';
// url could be yourdomain/imran10
$route['([a-zA-Z0-9]+)'] = "profile/index/$1";

你的课可能看起来像这样

class Profile extends CI_Controller {

    public function index($id)
    {
        // $id is your param
    }
}

更新:(小心)

请注意,如果您有一个班级Someclass,并且使用url yourdomain/Someclass,那么如果您有profile/index/$1,则会将其路由至$route['(:any)']$route['([a-zA-Z0-9]+)']

答案 1 :(得分:4)

您可以在路线文件中使用它:

$route['(:any)'] = "controller_name/$1";
$route['(:any)/(:any)'] = "controller_name/$1/$1";
$route['(:any)/(:any)/(:any)'] = "controller_name/$1/$1/$1";

答案 2 :(得分:2)

试试这个:

$route['user'] = 'profile/user';

不知道任何通用的方法

How to hide controller name in the url in CodeIgniter?

可能重复

答案 3 :(得分:0)

如果我理解你的问题,你只想在路线文件的最末端添加一个捕捉。像:

$route['(:any)'] = 'profile/index';

这将捕获未被其他路径捕获的所有内容,因此请确保此脚本包含用于确定何时应显示404的逻辑。

相关问题