Phil Young
Phil Young

Reputation: 1354

CodeIgniter routing not working

I am having problems with my routing. My default controller (mysite.com) will work, but if I try anything else (e.g. mysite.com/dashboard), it goes to a server based 404, not a CodeIgniter one. It's very confusing, as at the moment I only have 2 paths in my routes.php file. Here are the non-commented sections of my routes.php file:

$route['404_override'] = '';

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

My controller is located in /application/controllers/pages.php.

I don't think its a .htaccess issue (as it can get to the default controller), but here is my .htaccess file:

RewriteEngine On
RewriteCond $1 !^(index\.php|styles|scripts|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

#<IfModule mod_gzip.c>
#    mod_gzip_on       Yes
#    mod_gzip_dechunk  Yes
#    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl)$
#    mod_gzip_item_include handler   ^cgi-script$
#    mod_gzip_item_include mime      ^text/.*
#    mod_gzip_item_include mime      ^application/x-javascript.*
#    mod_gzip_item_exclude mime      ^image/.*
#    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
#</IfModule>

Edit

Here is the pages controller:

<?php

        class Pages extends CI_Controller {

        public function __construct()
        {       
                //Construct it's parent
            parent::__construct();

            //Check login
            //$this->load->model('pages_model');
        //$this->pages_model->getLoginStatus();

    }


    public function view($page = 'dashboard')
    {

        //If the file doesn't exist
        if ( ! file_exists('/var/www/vhosts/mysite/httpdocs/library/application/views/pages/'.$page.'.php'))
        {
            // Whoops, we don't have a page for that!
            show_404();
        }       

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

        //Load all necessary views
        $this->load->view('templates/head', $data); 
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);

    }

}

?>

Upvotes: 4

Views: 13908

Answers (3)

Valentino Pereira
Valentino Pereira

Reputation: 1325

Recently I upgraded my PHP version on the server from 7.0 to 7.2. However my CodeIgniter Routing stopped working after doing so. All requests were returning 300 error in browser console.

I resolved my issue by switching back to PHP 7.0.

Upvotes: 0

Alireza Soori
Alireza Soori

Reputation: 645

had your same problem just now , simply move .htaccess file to the root of your working directory
just take it out of application folder

Upvotes: 4

Laurence
Laurence

Reputation: 60040

This is my htaccess file that I use for ALL projects:

Options -Indexes
Options +FollowSymLinks

# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>

    # activate URL rewriting
    RewriteEngine on


    # do not rewrite links to the documentation, assets and public files
    RewriteCond $1 !^(images|assets|uploads|captcha)

    # do not rewrite for php files in the document root, robots.txt or the maintenance page
    RewriteCond $1 !^([^\..]+\.php|robots\.txt|maintenance\.html)

    # but rewrite everything else
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>

    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.

    ErrorDocument 404 index.php

</IfModule>  

Then make sure in your config.php file:

  $config['index_page'] = '';

Upvotes: 2

Related Questions