简体   繁体   中英

Codeigniter Routing Issues on AWS EC2

I have been searching the stackoverflow all day for an answer to this and I just can't seem to find one.

I have my Codeigniter app on an AWS EC2 server.

Here is my routes.php:

$route['default_controller'] = 'pages/home';
$route['(:any)'] = 'pages/$1';

EDIT Here is my current routes.php:

$route['default_controller'] = 'pages/home';
$route['about'] = 'pages/about';
$route['contact'] = 'pages/contact';
$route['home'] = 'pages/home';

Here is my Pages.php:

class Pages extends CI_Controller {

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

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

    public function about(){
        $this->view('about');
    }

    public function contact(){
        $this->view('contact');
    }

    public function view($page)
    {
         if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
        {
            // Whoops, we don't have a page for that!
            show_404();
        }

        $data['title'] = ucfirst($page); // Capitalize the first letter

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }

}

Here is my .htaccess:

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

My config.php:

$config['index_page'] = '';

PROBLEM

If I go to (my-url), it uses my default_controller. If I go to (my-url)/home or /about or /contact, I get a 404 error. If I go to (my-url)/index.php/home or /about or /contact, I get the default_controller.

I have double checked that mod_rewrite is installed and enabled. I can't seem to figure out why I'm not getting anything. I followed the tutorial and read a bunch of stackoverflow questions and answers to try to solve it, but to no avail.

Please help.

You can not use routes as like that:

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

It will not work with all functions as you need.

Just Suggestion:

Use your routes as:

$route['about'] = 'pages/about';
$route['contact'] = 'pages/contact';
$route['view/(:any)'] = 'pages/view/$1';
......

You need different routes for different URL if you use (:any) as first param it will not work properly for all functions.

Try this

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]

Note when using APPPATH constant, the path ends with '/' already

try change

if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))

to

if (!file_exists(APPPATH.'views/pages/'.$page.'.php'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM