How to Remove “index.php” in Codeigniter using htaccess

By: Sunil Kumar |  In: Website Optimization  |  Last Updated: 2018/05/27

How to Remove “index.php” in Codeigniter using htaccess

In CodeIgniter project the index.php file will be included in your URLs by default but who like the URLs with index.php.. ??

NO ONE !!

Even search engine doesn’t like it.

For making our URLs user-friendly and search engine friendly we have to remove this index.php from the website URL.

for eg. http://www.example.com/index.php/your_url

For removing –

1. create .htaccess file

Create a .htaccess file in your project’s root directory or CodeIgniter directory( not in the application directory).

A .htaccess file allows us to modify our rewrite rules without accessing server configuration files. For this reason, .htaccess is critical to your web application’s security. The period that precedes the filename ensures that the file is hidden.

htaccess is the short name used for Hypertext Access, which is a powerful configuration file that controls the directory “.htaccess”.

It is used by Apache-based web servers to control various server features.

After creating .htaccess file add following code to this file

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

First, allow changes in the .htaccess file. Open the default Apache configuration file using nano or your favorite text editor.

$ sudo nano /etc/apache2/sites-enabled/000-default.conf

inside that file add this line of code inside your <VirtualHost *:80> block-

<Directory "/home/testing/ci">

       Options Indexes FollowSymLinks MultiViews

       AllowOverride All

      Order allow,deny

      allow from all

      Require all granted

</Directory>

2. Enable mode_rewrite in apache

Now, we need to activate mod_rewrite

$ sudo a2enmod rewrite

restart Apache.

$ sudo service apache2 restart

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

3. Update config.php file

Now open config.php (path- application/config/config.php) in your text editor and search for following line-

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

and remove index.php from here as

$config['index_page'] = "";

But in some cases, it may happen that default setting for url_protocol does not work properly. For solving this problem again open config.php and look for code

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

and replace it with

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

Sometimes it may already set to REQUEST_URI, In this case, you don’t need to change it.

That’s It…!!!

Now your URL will be http://www.example.com/your_url which is more user-friendly and search engine friendly too.

Comments


  • Hi there would you mind sharing which blog platform you’re
    using? I’m looking to start my own blog soon but I’m having a difficult
    time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m
    looking for something unique.
    P.S Sorry for getting off-topic but I had to ask!

  • Great blog here! Additionally your web site a lot up very fast!

    What host are you the use of? Can I get your affiliate hyperlink on your host?
    I want my site loaded up as quickly as yours lol

  • Leave a Comment

    Your email address will not be published.

    *


    Sunil Kumar


    I am the owner of acmeextension. I am a passionate writter and reader. I like writting technical stuff and simplifying complex stuff.
    Know More

    Join more than 10,000 others Web Developers


    %d bloggers like this: