Remove index.php from URL in CodeIgniter
PHP CodeIgniter

Remove index.php from URL in CodeIgniter

Mishel Shaji
Mishel Shaji

After creating a CodeIgniter project, you might have noticed that index.php appears in the URLs of your website. In this tutorial, I will show you how to remove index.php from URL in CodeIgniter so that you will get a clean URL.

How to remove?

You can remove index.php from the URL by modifying the .htaccess file and the config file of CodeIgniter.

Step 1

  • First, create the .htaccess file at the root directory.
  • Paste the following code to the htaccess file and save it.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]

If the mod_rewrite option is not enabled in the Apache server, go to your apache’s httpd.conffile and search for the below code.

LoadModule rewrite_module modules/mod_rewrite.so

If the above line is preceeded with a # symbol,  remove the # (hash) symbol to enable URL rewriting.

If you are running on a Windows server (IIS), modifying or creating the .htaccess file won’t work. On a Windows server, place the following code to the web.config file.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite> 
    </system.webServer>
</configuration>

Step 2

  • Open config.php file under application->config.
  • Find the following line.

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

  • Remove index.php from the line.

$config['index_page'] = '';

This will now remove index.php from the URL.