How to remove index.php from URL in Codeigniter

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

For removing index.php from the URL you need to have .htaccess file under the project’s root directory and mod_rewrite enabled for your Apache HTTP Server. If you are on Windows Operating System then you may not be able to create a file which starts with a dot (.), so you need to use command line tool to create the file.

Prerequisites

Apache 2.4 HTTP Server, Codeigniter 3.1.11, PHP 7.4.3

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

Enable 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 if you do not have root access to your server.

<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 mod_rewrite through .htaccess file 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 the module enable.

Step 4: Find all occurrences of AllowOverride None and replace by AllowOverride All

Step 6: Now restart Apache 2.4 server

Update Config

Update the below configurations in 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';

Create .htaccess

Put the below lines in .htaccess file which is under project’s root directory.

In windows system you won’t be able to create .htaccess file normally. So you have to use cmd prompt to create .htaccess file. Use command echo > .htaccess in cmd prompt from the desired location 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-benchmarking.

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

#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 /codeIgniter-benchmarking/

# 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]

Now access the application URL http://localhost/codeIgniter-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.

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

Leave a Reply

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