1

How can I rewrite the following urls in CodeIgniter

localhost/users/show_profile/john

to

localhost/john

Thanks

sunjie
  • 555
  • 1
  • 4
  • 5

3 Answers3

2

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.

tpae
  • 6,286
  • 2
  • 37
  • 64
1

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)

minboost
  • 2,555
  • 1
  • 15
  • 15
0

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

Pav
  • 2,288
  • 4
  • 22
  • 25