codeigniter routes is not working

2020-05-03 11:06发布

问题:

I have the following problem with CodeIgniter. I'm trying to setup a menu, and using route config to load the proper content but from some reason it's not working.

I have CodeIgniter setup under:

http://localhost/new/CodeIgniter/

My config file looks like this:

$config['base_url'] = 'http://localhost/new/CodeIgniter/';
$config['index_page'] = '';

My route config looks like this:

$route['default_controller'] = 'Home/home';
$route['404_override'] = '';
$route['home'] = 'Home/home';
$route['compare'] = 'Home/home';
$route['signin'] = 'Home/home';
$route['translate_uri_dashes'] = FALSE;

And this is my Home controler:

class Home extends CI_Controller {

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

public function index() 
{
    $this->home();
}

public function home()
{
    $this->load->model('home_model');

    $this->load->view('header_view', $data);
    $this->load->view('nav_view', $data);
    $this->load->view('home_view', $data);
    $this->load->view('footer_view', $data);
}

public function compare()
{
    $this->load->model('home_model');

    $this->load->view('header_view', $data);
    $this->load->view('nav_view', $data);
    $this->load->view('home_view', $data);
    $this->load->view('footer_view', $data);
}

public function signin()
{
    $this->load->model('home_model');

    $this->load->view('header_view', $data);
    $this->load->view('nav_view', $data);
    $this->load->view('home_view', $data);
    $this->load->view('footer_view', $data);
}   

public function about()
{
    $this->load->model('home_model');

    $this->load->view('header_view', $data);
    $this->load->view('nav_view', $data);
    $this->load->view('home_view', $data);
    $this->load->view('footer_view', $data);
}   
}

When I access http://localhost/new/CodeIgniter/ I can see the home page, but none of the following are working:

http://localhost/new/CodeIgniter/home
http://localhost/new/CodeIgniter/compare
http://localhost/new/CodeIgniter/signin
http://localhost/new/CodeIgniter/about

Does anyone has any idea what I'm doing wrong here?

The error is the following:

Not Found

The requested URL /new/CodeIgniter/home was not found on this server.

This is my httaccess

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>
<IfModule ModRewite>
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

回答1:

I think a .htaccess file is missing. Create a new file with this content and save it in the CI root.

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


回答2:

Make sure you mod_rewrite enabled (check through phpinfo that is enable or not).

For enable mod_rewrite check following

  1. For Linux
  2. For window

And add this .htacess

where to add

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

in your root folder