sunjie
sunjie

Reputation: 565

Rewrite URLs in CodeIgniter

How can I rewrite the following urls in CodeIgniter

localhost/users/show_profile/john

to

localhost/john

Thanks

Upvotes: 1

Views: 1187

Answers (3)

tpae
tpae

Reputation: 6346

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.

Upvotes: 2

Pav
Pav

Reputation: 2318

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

Upvotes: 0

minboost
minboost

Reputation: 2563

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)

Upvotes: 1

Related Questions