• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with _remap function (trying to redirect) [SOLVED]

Hi all,

I have just started using CI about 2 days ago, and have come into a problem with the _remap function. I'm trying to redirect anything off the 'www.root.com/username' to 'www.root.com/profile/username', and have no idea where I'm going wrong - Any help will be appreciated.


class Home extends Controller {
    function _remap($username)
        $is_user = $this->Home_model->is_valid_user($username);
        echo $is_user;
        if($is_user == true):
            header('Location: /profile/' . $username);
            $data = array();
            $data['lastjoined'] = $this->Home_model->get_last_joined(3);
            $data['representing'] = $this->Autoload_model->get_socialnetworks('random',10);
            $data['randomtip'] = $this->Autoload_model->get_random_tip();
            $data['latestnews'] = $this->Autoload_model->get_latest_news(3,50);



class Home_model extends Model {
    function get_last_joined($limit = '10')
        $this->db->join('country', 'user.country_id = country.id');
        $query = $this->db->get();
        return $query->result();
    function is_valid_user($username = '')
        $this->db->where('user_name', $username);
        if($this->db->count_all_results() < 1):
            return false;
            return true;

Apologies for the crappy code ;p

the echo $is_user; was my attempt to debug, btw

hey there so you can't use the redirect() function?

in the url_helper class.

edit: like umm redirect('profile/'.$username); ?

also have you tested the is_valid_user() by itself? returning the right value?

u can test if ur redirect is working by hardcoding $is_user to true and see if it works?

Hi andrew,

I just tried the redirect - still no good Sad

I have tested is_valid_user() on it's own like this:

if($is_user == false):
echo 'false';
echo 'true';

it returned false on the homepage, and 404 when i tried to put a username after the www.root.com/

I think I'm definitely being insanely stupid somewhere here.

Thanks for your help,


Edit: I just tried hardcoding $is_user to true, and still got a 404 when i put a username on the end.

Don't you need a constructor?

www.root.com/username wouldn't work unless you were using a special route. _remap is only called if the constructor is called, so you'd need www.root.com/home to activate remap, and www.root.com/home/username to get it to redirect to profile/username.

www.root.com/username causes CI to look for the username controller unless you have routed it in some special way, which of course doesn't exist so 404.

Thanks BrianDHall.

So is there any other way to run the username off the root?



[quote author="tommizzle" date="1257458339"]Thanks BrianDHall.

So is there any other way to run the username off the root?



Sure, you need a sort of negative route. You define a route with regex that says "if I am not asking specifically for these controllers, then reroute it to this".

I just so happen to do exactly this. Here you go:

// Route everything but mentioned controllers to default controller, allowing short urls to work.
$route['^(?!controller|controller|controller).*'] = $route['default_controller'] . "/$0";

So stick in what you do not want rerouted, and then everything else will be rerouted to whatever you want. So long as there isn't someone with a username the same as one of your controllers (the easiest way to handle this is manually create a user with the username of each of your controllers, to reserve it and guarantee you never have a conflict), it should work as you think it would.

hey tom


under enabling query strings you could try to enable it so you can go


but other than that I think it's prob better to make a different controller because what you are affectively doing is logging in without a password yeh?

so you could do

root.com/profile/username then in ur "profile" controller have ur code there.. use the segment() function go get ur username and if it doesn't exist redirect them to home.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.