Thursday, December 1, 2022
HomeTutorialCodeigniterRemove Index.php From URL in Codeigniter 4 Framwork

Remove Index.php From URL in Codeigniter 4 Framwork

In this article we will remove index.php from URL in Codeigniter 4 framework. The directory structure of the New Codeigniter 4 framework has been changed. So you have noticed that no index.php in root directory.

You have also noticed that current url look like. Its look bad.

http://localhost.test/public/index.php/home
or
http://localhost.test/public/home

How to remove  public/index.php/  from URL in Codeigniter 4

Follow the steps and remove the public and index.php from URL in Codeigniter 4 framework:

Step 1: Change in App.php File

The first step is to open the app file. And make change mentioned below. You have to do this in your App.php file.

So go to project_name/app/Config/App.php and change mention below:

public $indexPage = 'index.php';
to 
public $indexPage = '';

Step 2: Create a new index.php & .htaccess file

Yes, we don’t copy or move public folders file in root folder. We will create new two files in root folder.

A. Create index.php file and paste below code in it and save it.

<?php 

   require_once __DIR__.'/public/index.php'; 

?>

 

B. Create .htaccess file and paste below code in it and save it.

<files .htaccess> 
order allow, deny 
deny from all 
</files>

DirectoryIndex index.php

<IfModule mod_rewrite.c>

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]

</IfModule>

So we have successfully removed index.php file in CodeIgniter4. If you have any question just drop the comment. You can use other contact mode to contact me. You can find it in on my Bio. Don’t forget to share with others.

Ankit Bhandari
Ankit Bhandarihttps://www.bhandariankit.com
My mission is to empower people to take control of their health and fitness by following a healthy, enjoyable lifestyle that not only gives them the body they’ve always wanted, but enables them to live a long, vital, disease-free life.
RELATED ARTICLES

Leave a Reply

Most Popular

Recent Comments