How to remove public/index.php/ from url in Codeigniter 4

How to remove public/index.php/ from url in Codeigniter 4

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

Stefan Bogdanescu

Founder & Senior Architect

Removing public/index.php from CodeIgniter 4 URLs

When deploying a PHP application, particularly one built on the CodeIgniter 4 framework, you often encounter URLs that expose the directory structure of your web server. Instead of seeing a clean route like www.sample.com/controller, users might be directed to www.sample.com/public/index.php/controller. While this behavior is functional, it breaks SEO requirements and creates inconsistent navigation for end-users. The solution lies in configuring your web server to rewrite requests so that the public/index.php file remains invisible while still being executed correctly.

This process relies on Apache's .htaccess configuration using mod_rewrite. The goal is to intercept any request that does not directly match an existing physical file or directory on the server. When such a request arrives, the server should internally redirect it to public/index.php, passing the original URI path as a query parameter. This allows your framework's routing logic to handle the URL without exposing the entry point.

The core configuration block required for this behavior is:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]

Here is a breakdown of how these directives interact to achieve the desired result. The first directive, RewriteEngine On, activates the runtime rewriting engine, which is necessary for subsequent rules to take effect. Without this flag enabled, Apache will ignore the following conditions and rules regardless of their correctness.

The next two lines use RewriteCond to establish conditional logic. The condition %{REQUEST_FILENAME} !-f checks if the requested filename does not exist as a regular file. Similarly, %{REQUEST_FILENAME} !-d ensures the request is not pointing to an existing directory. These conditions are critical because you want Apache to serve static assets like images or CSS files directly if they exist on the disk. If a user requests www.sample.com/assets/logo.png, the server should locate that file and serve it immediately, rather than routing it through public/index.php. By excluding existing files and directories from the rewrite rule, you preserve this standard web serving behavior.

If neither condition is met (meaning the file or directory does not exist), the RewriteRule executes. The pattern ^(.*)$ captures the entire requested URI path, including any query strings, into the backreference $1. This captured string is then appended to the path of your framework's entry point, resulting in a rewrite target of public/index.php/$1. The [L] flag signifies that this rule is the "Last" one to be processed for this specific request; if it matches, no further rewriting occurs.

This configuration effectively tells Apache: "If someone asks for something that isn't a real file or folder, send them to public/index.php, but make sure to pass along exactly what they asked for."

While this setup works seamlessly on Apache, it is important to note that the .htaccess file must reside inside the document root where the rewrites are intended to apply. If your application sits in a subdirectory, you may need to adjust the paths accordingly. Additionally, ensure that mod_rewrite is enabled on your server, as this is not active by default on all hosting environments.

For developers considering alternatives or looking for further optimization on their PHP stack, Laravel offers a similar approach to URL rewriting with its own .htaccess conventions. You can explore how other frameworks handle this at https://laravelcompany.com to see best practices across the ecosystem.

Our network