Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Htaccess and URL Redirect issues

I am trying to use .htaccess on CodeIgniter, but it is not working.

I have already set:

AllowOverride All
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

I am using XAMPP on Windows.

My .htaccess:

RewriteEngine On
RewriteCond $1 !^(index\.php|images|scripts|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

My URL still contains index.php and, if I try to remove it manually, it returns a 404 error

Also my other concern is my login page: whenever I login, my URL is stuck on the check page.

I was wondering how do I rewrite my URL to change to home page instead of staying on the check page.

public function check(){
        $this->load->model('check');
        $logcheck = $this->check->check($this->input->post('username'),$this->input->post('password'));

        if($logcheck == "admin"){
            $this->load->view('home');
        }else{
            $this->load->view('login');
        }
    }
like image 329
magicianiam Avatar asked Oct 19 '22 17:10

magicianiam


1 Answers

This simple .htaccess will remove your index.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Change your check function like this

 public function check(){
    $this->load->model('check');
    $logcheck = $this->check->check($this->input->post('username'),$this->input->post('password'));

    if($logcheck == "admin"){
        //may  be you need to set login credential into session
        redirect('Phonebook/home/');
        //$this->load->view('home');
    }else{
        $this->load->view('login');
    }
}

and your home function will be like this

public function home(){
      //remember you need to check login validation from session
      $this->load->view('home');
}

to use redirect function remember you have url helper loaded.
May be this help you

like image 171
Shaiful Islam Avatar answered Oct 22 '22 17:10

Shaiful Islam