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

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

Maybe you’re used to seeing the index.php in your URLs, and now there’s a new one?

NO ONE wants that! Even search engine bots have been trained not to look for it but rather any other file or folder name first.

To make your URL user-friendly, as well as SEO friendly, we need to delete this index.php from our website, addresses too – such as http://www/sample/index php//your url

For Removing:

htaccess

1. Create .htaccess file

A .htaccess file is a directory-based configuration file for Apache web servers.

It controls the “directory” by changing URL rewriting rules, redirecting requests, and more.

For this reason, it should be used to make sure our website’s security stays intact no matter what we do with code or design in the future.

After you have created the .htaccess file, include the code below in this file.

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

You need to allow changes in the .htaccess file. Use a text editor or nano to launch the default Apache configuration file.

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

Include this code inside this file <VirtualHost *:80> block-

<Directory "/home/testing/ci">

       Options Indexes FollowSymLinks MultiViews

       AllowOverride All

      Order allow,deny

      allow from all

      Require all granted

</Directory>

2. Allow mode_rewrite in apache

The next step is to activate mod_rewrite

$ sudo a2enmod rewrite

restart Apache.

$ sudo service apache2 restart

The module will be activated, and you will be notified.

3. Update config.php file

Next lauch the config.php (path- application/config/config.php) via your text editor to search for this-

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

and delete index.php here

$config['index_page'] = "";

However, some scenario may occur whereby that default url_protocol setting does not function rightly. To solve this issue, launch config.php and search for the code below

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

and replace with

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

There are times it may already be set to REQUEST_URI, so you do not have to make any changes.

Top Article:  8 Effective Tips To Make Your Website Appealing

Viola!

Now you will have your new URL as http://www.test.com/your_url, which looks simpler and search engine friendly.

Charlie has been building WordPress themes, reviewing web hosts and utilizing social media since their respective inceptions.

Leave a Reply