Home / Blog / How to Remove index.php from CodeIgniter URLs

How to Remove index.php from CodeIgniter URLs

CodeIgniter URLs can include index.php by default, which can make application URLs look unnecessarily long. Learn how to remove index.php from CodeIgniter URLs using Apache .htaccess, configure CodeIgniter correctly, troubleshoot common issues, and create cleaner, more user-friendly URLs.

How to Remove index.php from CodeIgniter URLs

How to Remove index.php from CodeIgniter URLs

CodeIgniter is a lightweight PHP framework widely used for developing dynamic web applications. One of its useful features is its simple URL structure, which makes applications easier to navigate and maintain.

However, a default CodeIgniter installation may generate URLs containing index.php, such as:

https://example.com/index.php/products

For many applications, developers prefer cleaner URLs such as:

https://example.com/products

Removing index.php makes URLs shorter and easier to read. It can also provide a cleaner structure for users and search engines.

In this guide, we will understand how to remove index.php from CodeIgniter URLs.

Why Remove index.php from CodeIgniter URLs?

The index.php portion is associated with CodeIgniter's front controller.

Although it works correctly, URLs without it are generally cleaner.

For example:

example.com/index.php/about

can become:

example.com/about

Some advantages include:

Cleaner URLs
Easier-to-read links
Better URL structure
More professional-looking websites
Easier URL sharing
Cleaner application architecture
Step 1: Configure CodeIgniter

First, open the CodeIgniter configuration file:

application/config/config.php

Find the following configuration:

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

Change it to:

$config['index_page'] = '';

This tells CodeIgniter that index.php should not be included in generated URLs.

Step 2: Create an .htaccess File

The next step is to configure Apache's rewrite rules.

Create a file named:

.htaccess

in the root directory of your CodeIgniter application.

For example:

your-codeigniter-project/
├── application/
├── system/
├── index.php
└── .htaccess
Step 3: Add Rewrite Rules

Add the following rules to the .htaccess file:

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

These rules tell Apache to route requests through CodeIgniter's front controller without requiring users to include index.php in the URL.

Step 4: Check Apache mod_rewrite

The URL rewriting functionality depends on Apache's mod_rewrite module.

Make sure it is enabled on your server.

On supported Linux environments, you may need to enable it using:

sudo a2enmod rewrite

Then restart Apache:

sudo service apache2 restart

The exact commands can vary depending on the operating system and server configuration.

Step 5: Configure AllowOverride

Apache must also allow .htaccess rules to be applied.

Check the relevant Apache virtual host or directory configuration.

A typical configuration may look like:

<Directory /var/www/html>
    AllowOverride All
</Directory>

After changing Apache configuration, restart the web server.

Step 6: Test Your URLs

Suppose your CodeIgniter application has an About page.

Before the configuration, the URL might be:

example.com/index.php/about

After configuration, it should be accessible through:

example.com/about

Test several routes to make sure the rewrite rules are working correctly.

Alternative .htaccess Configuration

Depending on your CodeIgniter version and server configuration, you may also use:

RewriteEngine On

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

If your application is installed inside a subdirectory, the rewrite configuration may need to be adjusted accordingly.

Removing index.php From CodeIgniter in a Subdirectory

Suppose your application is installed at:

example.com/myapp/

The .htaccess file should be placed appropriately within the application directory.

You may use:

RewriteEngine On
RewriteBase /myapp/

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

The correct RewriteBase depends on your server and application configuration, so it should be tested in the actual hosting environment.

Common Problems After Removing index.php

Sometimes the configuration does not work immediately.

Here are some common problems.

1. 404 Errors

If URLs return a 404 error after removing index.php, check whether Apache's rewrite module is enabled.

Also verify that your .htaccess file is in the correct directory.

2. .htaccess Is Not Working

Apache may not be allowing overrides.

Check:

AllowOverride All

for the relevant directory.

Also make sure Apache is configured to read .htaccess files.

3. CSS and JavaScript Files Stop Loading

If CSS and JavaScript files are not loading correctly, check your application's base URL and asset paths.

In CodeIgniter, verify:

$config['base_url'] = 'https://example.com/';

Use appropriate asset URLs rather than relying on relative paths that may change depending on the current route.

4. Application Works With index.php but Not Without It

If:

example.com/index.php/about

works but:

example.com/about

does not, the issue is usually related to URL rewriting or Apache configuration.

Check:

mod_rewrite
.htaccess
AllowOverride
DocumentRoot
CodeIgniter index_page
Apache configuration
5. Hosting Environment Does Not Use Apache

The .htaccess method applies to Apache-based hosting.

If your application uses Nginx, IIS, or another web server, the rewrite configuration needs to be implemented using that server's configuration mechanism.

CodeIgniter URL Configuration Best Practices

Removing index.php is only one part of creating clean application URLs.

Consider the following practices.

Keep URLs Simple

Use meaningful paths such as:

/products
/services
/about-us
/contact

instead of unnecessarily complicated URLs.

Use Meaningful Route Names

URLs should help users understand what the page represents.

Avoid Unnecessary Parameters

Where possible, avoid unnecessarily long query strings.

Maintain Consistent URL Structure

Use a predictable structure throughout the application.

Redirect Old URLs

If an existing application has already indexed URLs containing index.php, consider implementing appropriate redirects rather than simply removing the old URLs.

This helps preserve existing links and minimizes broken references.

Is Removing index.php Necessary?

No. Your CodeIgniter application can work perfectly well with index.php in its URLs.

Removing it is primarily a URL-cleanliness and routing preference.

It becomes particularly useful when you want a cleaner public URL structure and a more polished application architecture.

Conclusion

Removing index.php from CodeIgniter URLs is relatively straightforward when Apache URL rewriting is correctly configured.

The main steps are:

Set the CodeIgniter index_page configuration to an empty string.
Create an .htaccess file.
Add the appropriate rewrite rules.
Make sure Apache's rewrite functionality is enabled.
Ensure .htaccess overrides are permitted.
Test all application routes.

A clean URL structure can make a CodeIgniter application easier to use, share, maintain, and manage. However, the exact configuration depends on the CodeIgniter version, hosting environment, and web server being used.

Contact Us

1119 W Duarte Rd, Arcadia, CA 91007

Solace Infotech Pvt. Ltd, Supreme HQ,
          HQ3C+9F2, Yash Orchid Society,
          Baner, Pune, Maharashtra 411021

4th Floor, Samraat Nucleus,
           Mumbai Naka, Nashik - 422001