yab86 yab86 - 16 days ago 8
PHP Question

Codeigniter call Function in Controller without index.php

I know there are so many posts concering that problem. I tried everything, but without success.

I use xampp v3.2.2 on my windows 10 machine. In my htdocs I got a project called mysite. In there I have codeigniter 3.0.3.

config.php

$config['base_url'] = 'http://localhost/mysite/';

$config['index_page'] = '';


routes.php

$route['default_controller'] = 'CI_home';


Controller CI_home.php:

class CI_home extends CI_Controller{

function __construct() {
parent::__construct();
}

public function index($lang = ''){
$this->load->helper('url');
$this->load->helper('language');
$this->lang->load($lang, $lang);
$this->load->view('view_home');
}
}


When I call
http://localhost/mysite
, the page is shown correctly.

The problem is, when I try to
http://localhost/mysite/en
or
http://localhost/mysite/index/en
I received a 404 from xampp.

But if I try
http://localhost/mysite/index.php/CI_home/index/en
it works fine.

What I am doing wrong? How can I remove the "CI_home/index"?

http://localhost/mysite/.htaccess:


.htaccess

RewriteEngine On
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]


I already checked if the mod_rewrite is enabled.

Please help me!

Thanks in advance,
yab86

Answer

Try this htaccess below

Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Make sure htaccess in your main directory.

Then set your base url

$config['base_url'] = 'http://localhost/yourproject/';

Then remove index.php

$config['index_page'] = '';

Note: Codeigniter 3 versions are case sensitive. Make sure only the first letter upper case of class and file name.

<?php

class Ci_home extends CI_Controller {

    public function __construct() {
         parent::__construct();
    }

     public function index($lang = '') {
       $this->load->helper('url');
       $this->load->helper('language');
       $this->lang->load($lang, $lang);
       $this->load->view('view_home');
     }
}

Then make sure file name is Ci_home.php

Then your default route should be

When using default controller make sure is the same name as the controller you choose.

URI Routing

$route['default_controller'] = 'ci_home';

$route['(:any)'] = 'ci_home/index/$1';