404 page not found error in Codeigniter

2019-08-01 23:01发布

问题:

I am new to the codeigniter framework and I have tried to make my first program but received a 404 page not found error

This is my root directory Codeigniter and my directory structure

My source folder contains the .htaccess file containing the following code

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

Home.php

    class  Home extends CI_Controller{
    public function index(){
        $this->load->view('View');
    }
}

view.php

echo "HI this is my first codeigniter program";

I have tried the following url http://localhost/Codeigniter/ but I get a 404 error, however http://localhost/Codeigniter/home loads the correct result even though my actual root folder is Codeigniter

how to solve this problem?

回答1:

Change route file in this following path:-

CodeIgniter/application/config/routes.php

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

Then only you can access the following url

http://localhost/Codeigniter/


回答2:

Try where

RewriteRule .* index.php/$0 [PT,L] 

change to

RewriteRule .* index.php/$1 [PT,L]

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]

Or

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]

More htaccess here

Make sure your htaccess is outside of application folder.

And then on config.php

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

$config['index_page'] = '';

And then you can set your routes.php lower case best on routes.php

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