简体   繁体   English

如何在CodeIgniter中更改URL?

[英]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. 我必须将www.sample.com/users/user/username更改为www.sample.com/username ,但是当我转到sitename/username它会显示404页面。 I've tried to route, and uri->segment , but I can't get this to work. 我试图路由,并且uri->segment ,但我不能uri->segment工作。 Please help me with this issue! 请帮我解决这个问题!

Add this line to your config/routes.php file. 将此行添加到config/routes.php文件中。

$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: 在您的system/application/config/routes.php ,添加以下内容:

$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: 请注意,您需要将<otherController1>|<otherController1>替换为其他实际控制器的实际名称,例如:

$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. 并且因为您不希望Users控制器的其他方法与用户名冲突,所以我们需要路由所有其他Users控制器方法。 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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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