Programming Codeigniter How to remove index.php from CodeIgniter URL in steps?

How to remove index.php from CodeIgniter URL in steps?

How to remove index.php from CodeIgniter URL in steps

CodeIgniter has a main file index.php, which is the entry point for any request. The default URL pattern in CodeIgniter is youwebsite/index.php/controller/action. This does not look good at all and doesn’t seem to be a standard URL format. Moreover, if you will use URL like youwebsite/controller it will display “404” server error. The server will not find the requested URL. Here is the short tutorial on How to remove index.php from CodeIgniter URL. After this, you will be able to change the CodeIgniter URL pattern. The new URL will look like youwebsite/controller/action.

Remove index.php from CodeIgniter URL using .htaccess file

Before working with the .htaccess file, let’s get a brief introduction about this file. Newbie developers may not be able to understand these in one go. However, practicing more and more will you perfect in this. When I started, .htaccess was really a nightmare for me. But now I am pretty much comfortable to solve all the issues related to it.

What is .htaccess file?

.htaccess is a configuration file. This is used on web servers that are running the Apache Web Server software. If there is a .htaccess file present in a folder which is going to be loaded via the Apache Web Server. Then Apache Web Server software detects that file and executes that file at very first. After that other PHP, HTML, etc. codes will be executed.

You can simply consider this as the main gate of a society, which you will access at the very first point. There you will get information about the block number and routes to follow.

Steps to remove index.php from CodeIgniter URL by .htaccess file

Below are only three very easy steps to create a .htaccess file. Follow them and your website will get standard URL formats.

Step #1: Create a new file in the project folder

Create a new blank file in the project folder. In my case, I am creating this file in cidemo folder, which is my project directory. To do so, I am using Sublime text 3.

create-new-htaccess-file-in-codeigniter-techbriefers

Step #2: Write .htaccess code to rewrite URLs

Below are the lines of codes which you need to put in that newly created file. In this code I am using RewriteRule ^(.*)$ index.php/$1 [L]. This means any URL component for this folder/website will be followed by index.php/. So the CodeIgniter will get everything as it requires by internal functionality of .htaccess.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Code in htaccess file to remove index.php from CodeIgniter

Step #3: Carefully save the file .htaccess

Now save the file with name ‘.htaccess’. This file must not have any name section. If you are using any text editor, make sure that it should not change the file extension. This file will be with no name and .htaccess extension only.

Save htaccess file to remove index.php from CodeIgniter
created htaccess file to remove index.php from CodeIgniter

Step #4: Change the Index File in Codeigniter configuration

One last thing to do is to change the Index file in Codeigniter configuration. To do this, open application/config folder. Scroll down and find $config['index_page']. It has a default value assigned. You need to remove that value.

Index File is set to index.php by default

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = 'index.php';

Remove this index.php and assign a blank value

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';

You are done and the file is successfully created. Now you can access any URL in your website without index.php in between.

4 thoughts on “How to remove index.php from CodeIgniter URL in steps?

  1. Thanks to my father who stated to me regarding this weblog, this weblog is in fact awesome.

  2. There is certainly a lot to know about this topic.

    I like all the points you have made.

Average 
 5 Based On 2

Leave a Reply