Enabling Clean URLs for CodeIgniter

CodeIgniter by default will have to first load the index.php page which would then further load the rest of the pages. This can be removed from the URL simply by following these steps:

  1. Create a new file in the CodeIgniter root folder named .htaccess with the following contents.
    <IfModule mod_rewrite.c="">
        RewriteEngine On
        RewriteBase /
        #Removes access to the system folder by users.
        #Additionally this will allow you to create a System.php controller,
        #previously this would not have been possible.
        #'system' can be replaced if you have renamed your system folder.
        RewriteCond %{REQUEST_URI} ^system.*
        RewriteRule ^(.*)$ /index.php?/$1 [L]
        #When your application folder isn't in the system folder
        #This snippet prevents user access to the application folder
        #Submitted by: Fabdrol
        #Rename 'application' to your applications folder name.
        RewriteCond %{REQUEST_URI} ^application.*
        RewriteRule ^(.*)$ /index.php?/$1 [L]
        #Checks to see if the user is attempting to access a valid file,
        #such as an image or css document, if this isn't true it sends the
        #request to index.php
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?/$1 [L]
    <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.
        # Submitted by: ElliotHaughin
        ErrorDocument 404 /index.php

    If CodeIgniter is not the webroot and is a subfolder then alter the line RewriteBase / to RewriteBase /[Sub Folder Name]/ where [Sub Folder Name] should be replaced by the actual name of the folder.

    Note: In case the above file doesn’t work, remove the <IfModule> opening and closing tags to test if for some reason Apache wasn’t able to load the module.

    Save the file.

  2. Open the file inside the CodeIgniter folder in the path application/config/config.php and alter the following line
    $config['index_page'] = 'index.php';

    Remove the ‘index.php’ to (empty string) and save.

  3. Enable the mod_rewrite module of Apache using the following command on Ubuntu:
    sudo a2enmod rewrite

    The Apache needs to be restarted before changes can take affect.

  4. Make sure the required settings are made in the sites file. In Ubuntu this file can be found at /etc/apache/sites-available/default and make the following changes:
           Options FollowSymLinks
           AllowOverride FileInfo
  5. Restart Apache using the following command:
    sudo service apache2 restart

Now test the website by using the URL without the index.php word and it should work fine.