CodeIgniter routing not working

13,350

Solution 1

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

Solution 2

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'] = '';
Share:
13,350
Phil Young
Author by

Phil Young

Web programmer, specialising in PHP and MySQL.

Updated on June 04, 2022

Comments

  • Phil Young
    Phil Young almost 2 years

    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);
    
        }
    
    }
    
    ?>
    
  • Phil Young
    Phil Young about 12 years
    Thanks @Laurencei but we now know the server isn't allowing the redirect but I will use this when I sort that out! Thanks :)
  • gumuruh
    gumuruh over 2 years
    where did you put that htaccess file anyway? @Laurence
  • Laurence
    Laurence over 2 years
    @gumuruh - it goes into the base of your public www folder. Wherever your index.php file is.