How to remove index.php from URL in CodeIgniter?

Remove Index.php codeigniter

Codeigniter is one of the most popular MVC frameworks in the world. If you are building any application with Codeigniter, You will notice index.php included in the URL by default. The codeigniter framework show all the page through one file index.php. Without this index.php file, any codeigniter function won’t work.

When you want to rank your website on google, you need to create a user-friendly and search engine friendly URL. We need to remove index.php from the URL, So that the url becomes search engine friendly and looks clean. But we will need to remove index.php from the URL with proper configuration or it will show error 404 page.

Default Codeigniter URL: https://example.com/index.php/test

Search Engine Friendly URL: https://example.com/test

In this tutorial, I will explain how to remove index.php from URL using htaccess in Codeigniter. Let’s start

At first, We need create a htaccess in youe application root directory. htaccess file will allow us to modify rewrite rules without accessing server configuration files. .htaccess is a configuration file for use on web servers running the Apache Web Server software. So don’t add any code this htaccess file without any knowledge otherwise your application won’t work.

Steps To Remove index.php using .htaccess:

Step 1: After creating .htaccess in your application root directory, Add the following code in this file.

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

Step 2:  Open the file config.php located in application/config path.  Find and Replace the below code in config.php  file.

Find the below code:

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

Replace it as:

$config['index_page'] = '';

Step 3: In some cases, the default setting for uri_protocol does not work properly. To solve this issue just open the file config.php located in application/config and then find and replace the code as:

Find the below code:

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

Replace it as:

$config['uri_protocol'] = "REQUEST_URI";

Sometime it’s already set to REQUEST_URL. In this time, We don’t need to remove it.

Conclusion:

I hope these steps helped you to remove index.php in the CodeIgniter framework using .htaccess. If you have any questions about Codeigniter, Please comment below. I will always try to reply to your comment. Keep reading our blogs. Thank You.

Leave a Reply

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