This tutorial shows how to remove index.php from URL in Codeigniter. We want to remove the index.php file so that URL looks good to make a clean URL and it makes better readability. Also the clean URL is easy to remember.

Prerequisites

Enablement of mod_rewrite

Apache 2.4 HTTP Server, Codeigniter 3.1.10, PHP 7.3.5

Example with Source Code

Before you change anything in comfig.php and .htaccess file make sure you have mod_rewrite enabled in the server otherwise it won’t work.

Enabling mod_rewrite

For localhost you can enable it yourself and for live server if it does not work then you can contact with the hosting company for enabling the mod_rewrite.

<root directory> – is the project’s root directory under htdocs in localhost or public_html(live server).

Find the following steps to enable mod_rewrite in localhost Apache 2.4 http server.

A rewrite rule can be enabled through in httpd.conf or in .htaccess file. Here are the instructions on how to enable .htaccess mod_rewrite in Apache 2.4 server.

Step 1: Go to the conf directory of Apache server installation: Apache24\conf, where Apache24 is the installation root directory of Apache 2.4.
Step 2: Open httpd.conf in a text editor.
Step 3: Find the line which contains #LoadModule rewrite_module modules/mod_rewrite.so and remove (#) from start of line to make module enable.
Step 4: Find all occurrences of AllowOverride None and replace by AllowOverride All
Step 6: Now restart Apache 2.4 server

Updating Config

Update the below configurations in <root directory>/application/config/config.php file.

Notice in the below configuration we have removed index page and we make the uri_protocol as AUTO.

$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

Creating .htaccess

Put the below lines in .htaccess file which is under <root directory>.

In windows system you won’t be able to create .htaccess file normally. So you have to use cmd prompt to use .htaccess file. Use command echo > .htaccess in cmd prompt to create .htaccess file.

#Initialize mod_rewrite
RewriteEngine On

# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/site_folder/, use /site_folder/
RewriteBase /<root directory>/

# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#For reuests that are not actual files or directories,
#Rewrite to index.php/URL
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]

Testing the Application

Let’s say we want to remove index.php from Benchmarking Codeigniter Application, so we need to put the .htaccess file under project root directory codeIgniter-3.1.10-benchmarking.

Now you need to change one line in the above .htaccess file content. Replace RewriteBase /<root directory>/ by RewriteBase /codeIgniter-3.1.10-benchmarking/.

Now access the application URL http://localhost/codeIgniter-3.1.10-benchmarking/benchmarking in the browser, where benchmarking is the controller name. You will see the below page:

remove index.php from url

Now you see in the above image there is no index.php in the URL.

That’s all. Thanks for reading.

Tags:

2 thoughts on “How to remove index.php from URL in Codeigniter

  1. I needed to thank you for this great read!! I certainly loved every bit
    of it. I have you bookmarked to look at new stuff you post…

Leave a Reply

Your email address will not be published. Required fields are marked *