Removing index.php from CodeIgniter URLs

Removing index.php from CodeIgniter URLs

If you have developed CodeIgniter application and want to remove the index.php file from the application generated URLs, you can follow this simple steps.

As a first step you need to go to the config file and remove the index.php. If you have deployed the website to apache web server please make sure the mod_rewrite module is enabled. It is the case with most of the hosting providers.

In CodeIgniter 4


public $indexPage = '';

In CodeIgniter 3 and earlier versions


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

If you have deployed the website with nginx server the below step is required and your website should already work with above changes. If you have deployed in apache then create .htaccess file in the website root directory and add below contents to that.

RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Now test the website and it should be working without index.php file. If you are using nginx you can route all the traffic through the php fpm process so that the url without php extension also will be handled as expected. Below is sample server block for nginx to use without index.php in the URL. Please note the bolded section in the server block.

server{
    server_name     aboutfullstack.com;
    root            /opt/www/$server_name/;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm.socket;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Hope this helped to implement the change. You can comment if you have any question related to this.

A full stack developer learning a developing web applications since my university time for more than 20 years now and Pega Certified Lead System Architect (since 2013) with nearly 16 years experience in Pega.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top