PHP and MySQL (56 Blogs) Become a Certified Professional

How to remove index.php from URL in CodeIgniter?

Last updated on Nov 25,2020 75K Views

How to remove index.php from URL in CodeIgniter?

In the CodeIgniter project, the index.php file will be included in the URL by default but to make it search engine friendly and user-friendly, we need to remove it. In this article, we will learn how to remove index.php in Codeigniter.

Following pointers will be covered in this article:

Let’s begin.

What is CodeIgniter?

CodeIgniter is a PHP driven framework for developing applications rapidly. While building a web application, we spend a lot of time writing the same code again and again. Frameworks provide us a starting block and minimize the amount of code needed to build a website. CodeIgniter is a free, open-source, easy-to-use, object-oriented PHP web application framework. CodeIgniter is based on the MVC, it has libraries for connecting to the database and performing various operations like sending emails, uploading files, etc.

For using Codeigniter, it is good to have some basic knowledge of PHP syntax and how to interact with databases and HTML.

Why remove index.php?

Removing index.php makes the URL look cleaner and professional. The Codeigniter framework served all the views through the one file index.php. Without this file, no model/views/controller won’t work. We need to remove the index.php from URL with proper configuration or else it will show 404 page.

How to remove index.php?

1. Create a .htaccess file

Firstly we will need to create a .htaccess file in our project’s root directory or CodeIgniter directory. Creating a .htaccess file will allow us to modify our rewrite rules without accessing server configuration files. Because of this reason, .htaccess is critical to our web application’s security thus ensures that the file is hidden. htaccess is an acronym used for Hypertext Access, it is a configuration file that controls the directory “.htaccess”.

After creating the .htaccess file, add the following code to this file.

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

2. Removing index.php file

Open the config.php file in your text editor and remove index.php from here.

Change:

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

To:

$config['index_page'] = '';

But in a few cases, it may happen that the default setting for url_protocol does not work properly. For solving this problem we need to open config.php and

Change:

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

To:

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

Most of the time it is already set to REQUEST_URI, In that case, we don’t need to change it.

3. Enable apache mode rewrite

Now, we will need to activate the mod_rewrite with the help of the following command.

$ sudo a2enmod rewrite

 

4. Restart Apache

Now we need to restart the Apache, to do so we write the following command.

$ sudo service apache2 restart

This will activate the module or alert us that the module is already in effect. 

Now with this, we have come to the end of the article. I hope you guys enjoyed it and understood the concepts of PHP. So, with the end of this tutorial, you are no longer a newbie to the scripting language.

If you found this PHP Tutorial blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”How to remove index.php in Codeigniter” and I will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to remove index.php from URL in CodeIgniter?

edureka.co