3
votes

I now set uri routing in my codeigniter config like this

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

and everything works fine with request like this

http://localhost/user/1

but if i try request

http://localhost/user/

so uri segment is empty, i am getting error

404 Page Not Found
The page you requested was not found.

how to avoid this? I ve tried to use

$user = $this->uri->segment(2);
if (!isset($user)) redirect('/', 'refresh');

but this is not working.

4
Just out of curiosity, what is the '1' in the second segment for? - Marc Audet
I think 1 can be only... administrator ;D - Derfder
Just string now to test output, planning name/id - MyMomSaysIamSpecial
Still not working? Have you tried $route['user'] = 'users'; $route['user/(:any)'] = 'users/index/$1'; ? What is var_dump($slug); die(); in your index method in your controller users echoing when you access it like http://something.com/user/2? - Derfder

4 Answers

4
votes

You can add another route such as:

$route['user'] = "users"
4
votes

build your functions in your controller instead of this:

public function index()
{
...

try something like this:

public function index($slug = '')
{

if ($slug = '') {
   // redirect code here
}
else {
   // if there is something in $slug then load this code
}

and maybe your routes should be something like:

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

Try with

$route['user:any'] = "users";
1
votes

I think you need to fix the syntax, please try:

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

You need parentheses around the wildcard (:any).

Reference: http://ellislab.com/codeigniter/user-guide/general/routing.html see Examples