简体   繁体   中英

How to Change URL in CodeIgniter?

I have to change www.sample.com/users/user/username to www.sample.com/username , but when I go to sitename/username it displays a 404 page. I've tried to route, and uri->segment , but I can't get this to work. Please help me with this issue!

Add this line to your config/routes.php file.

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

Note that if you do this, you'll have to manually add exceptions for other controllers/actions to work. So if you have a posts controller, your routes file is now:

$route['posts'] = "posts";
$route['posts/(:any)'] = "posts/$1";
$route['(:any)'] = "users/user/$1";

And you'll have to do this for every controller/action you have that's not users/user. You would also have to prevent usernames from being the same names as your other controllers/actions also.

In your system/application/config/routes.php , add this:

$route['^(?!<otherController1>|<otherController2>).*'] = "users/user/$1";

Please note that you need to replace the <otherController1>|<otherController1> with actual names of your other actual controllers, like:

$route['^(?!welcome|post).*'] = "users/user/$1";

And because you wouldn't want the other methods of your Users controller to collide with the usernames, we need to route all the other Users controller methods. For example:

$route['user/subscribe']            = "user/subscribe";
$route['user/username_exists']      = "user/username_exists";
$route['user/email_exists']         = "user/email_exists";
$route['user/signup']               = "user/signup";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM