How to remove "index.php" in codeigniter's path

How to remove "index.php" in codeigniter's path

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Removing index.php from CodeIgniter URLs

CodeIgniter defaults to a "double-pass" architecture where every request hits index.php, which then routes the request to the appropriate controller. While this offers some inherent protection against direct file access, it often results in non-clean URLs that look like example.com/index.php/user/profile. If you want those clean paths like example.com/user/profile, you need to reconfigure the web server's URL rewriting rules. This process involves two distinct layers: enabling the rewrite engine and mapping the requested URI to the correct CodeIgniter entry point.

Enabling Mod_Rewrite

Before any routing can occur, your web server must accept .htaccess directives or virtual host configurations that allow it to interpret rewrite rules. The most common scenario is using Apache with the mod_rewrite module.

You must first ensure that mod_rewrite is enabled. On Debian-based systems, this is often done with:

sudo a2enmod rewrite
sudo systemctl restart apache2

For Nginx, you don't need to enable a module, but you do need to configure the server block to pass requests through PHP's FCGI or FastCGI interface before hitting the application. If you are using Nginx, the location block usually looks like this:

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

This configuration attempts to serve the file directly if it exists, serves a directory listing for folders, and finally falls back to index.php with the original query string intact. This fallback is crucial because CodeIgniter relies on parsing the query string to determine which controller and method to invoke.

Configuring the .htaccess File

Once the rewrite engine is active, you need to add rules to your root .htaccess file (or equivalent virtual host config). The goal is to strip index.php from the URL if it exists, or redirect clean URLs to the internal entry point.

Here is a standard configuration block for Apache:

RewriteEngine On
RewriteBase /

# If an actual file or directory exists, serve it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Route everything else to index.php
RewriteRule ^(.*)$ index.php/$1 [L]

The first two conditions ensure that static assets (images, CSS, JS) and existing directories are not rewritten. If a request comes in for example.com/assets/logo.png, the server serves it directly without involving PHP. This prevents unnecessary processing and allows caching to work effectively for static files.

When you remove index.php from the URL, the web server passes the clean path to the script. Inside CodeIgniter's routing mechanism, the framework examines the URI segment. By default, CodeIgniter expects the first segment of the URI to be the controller name. If your URL is /user/profile, the router maps this to the User controller and the profile method.

Handling Legacy Code and Edge Cases

One significant pitfall when transitioning from default URLs to clean URLs is legacy links or bookmarks. Old bookmarks pointing to example.com/index.php/site/about will break unless you handle them explicitly. You can add a rule to redirect requests containing index.php to the same path without it:

RewriteCond %{REQUEST_URI} ^(.*)/index\.php(.*)$
RewriteRule ^ /%1 [R=301,L]

This rule checks if the URI contains /index.php and issues a 301 redirect to the new clean path. The %1 placeholder preserves the original query string, ensuring that parameters like ?id=5 are passed along correctly.

Another consideration is security. While removing index.php improves aesthetics, it does not inherently increase security against direct file access. You must still restrict access to sensitive files like .htaccess, .env, or backup folders (e.g., *.bak, *.sql) using Deny from all rules within your configuration.

If you are migrating from a different framework or upgrading an existing application, ensure that your database migrations and asset pipelines account for the new routing structure. Sometimes, hard-coded URLs in views or external scripts need to be updated via find-and-replace operations or by utilizing helper functions that automatically generate the correct base URL.

Our network