我有一个名为“个人资料”的控制器,用户个人资料的网址是
www.example.com/profile/用户

我已经在路由文件中使用了codeigniter的重新路由

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


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

例如

www.example.com/user

有什么建议?

最佳答案

您可以尝试任何一种

// 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]+)']

关于php - 如何从codeigniter中的URL中删除 Controller 名称?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19786855/

10-14 15:22