Issues in removing index.php in CodeIgniter 3

前端 未结 10 1437
灰色年华
灰色年华 2021-01-13 06:53

I want to access my URL\'s without index.php in CodeIgniter. Here is my Blog controller

class Blog extends CI_Controller {

    pub         


        
10条回答
  •  萌比男神i
    2021-01-13 07:35

    I just now struggled with this part again, even after being used to Codeigniter for 2+ years.

    None of the answers helped me exactly, including the official Codeigniter's page about this, so I'm posting the solution that worked for me.

    1. Enable Rewrite engine

      sudo a2enmod rewrite
      
    2. Change Apache's config file to allow folders to allow overriding of default security setting

      sudo nano /etc/apache2/apache2.conf
      

    Under the "Directory" options depending on the location of your files, edit

        AllowOverride None
    

    to

        AllowOverride All
    

    e.g.: My server files are in "/var/www", so my final result for corresponding Directory options is:

        
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        
    
    1. Create a ".htaccess" file in the root of your Codeigniter project, i.e.: along with where applications, system folders exist.

    In the file, put the following:

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
    
    1. In config/config.php file, change:

      $config['index_page'] = 'index.php';
      

    to

        $config['index_page'] = '';
    
    1. Restart apache2:

      sudo service apache2 restart
      

    Enjoy!

提交回复
热议问题