Rewrite URLs in CodeIgniter

2019-08-05 13:11发布

问题:

How can I rewrite the following urls in CodeIgniter

localhost/users/show_profile/john

to

localhost/john

Thanks

回答1:

you can achieve dynamic URL using CodeIgniter Routes.

Assuming localhost/users/show_profile/john

We're looking at:

localhost/controller/method/variable

we can use the following route:

$route['([a-zA-z_]+)'] = "users/show_profile/$1";

You can access the variable by calling $this->uri->segment(1); inside show_profile() function.

IMPORTANT: keep the $route['([a-zA-z_]+)'] at the end of the routes.php file, to make sure it's not overriding any other routes.



回答2:

If you only need to do it for a defined set of URLs, update /config/routes.php

If you need it to be dynamic then you should extend the CodeIgniter Router Library.

http://codeigniter.com/user_guide/general/creating_libraries.html (look for Extending Native Libraries)



回答3:

In config/routes.php add $route['(:any)'] = "users/show_profile/$1";
More info on routes here



标签: codeigniter