2

I have controller named “profile” and the URL for user profile is www.example.com/profile/user

I am already using the rerouting of codeigniter in the routes file

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

what I am looking is to remove the profile controller name from the URL so it will be SEO and user friendly.

e.g

www.example.com/user

any suggestions?

4

4 回答 4

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并且你使用urllike ,那么如果你有or yourdomain/Someclass,这将被路由到。profile/index/$1$route['(:any)']$route['([a-zA-Z0-9]+)']

于 2013-11-05T10:49:24.250 回答
4

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

$route['(:any)'] = "controller_name/$1";
$route['(:any)/(:any)'] = "controller_name/$1/$1";
$route['(:any)/(:any)/(:any)'] = "controller_name/$1/$1/$1";
于 2014-05-20T20:56:51.313 回答
2

尝试这个:

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

不知道任何通用的方法来做到这一点

如何在 CodeIgniter 的 url 中隐藏控制器名称的可能重复项

于 2013-11-05T10:44:07.243 回答
0

如果我正确理解您的问题,您只想在路线文件的最后添加一个总括。喜欢:

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

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

于 2013-11-05T10:43:14.793 回答