CodeIgniter Laravel PHP Example HTML Javascript jQuery MORE
Need a Website Or Web Application.Contact : +91 9437911966 (Whatsapp) Note: Paid Service

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


Using htaccess

It is very easy to remove remove public/index.php/ from url in Codeigniter 4.

You need to create 2 .htaccess files.

The first one in root path "/.htaccess" of your project the second one in the public folder "public/.htaccess"

and here is the first one "/.htaccess"

# this is my version (Nassim) of the htaccess file , I use this one in the root directory "/"

# of the project and a second .htaccess in the public directory

DirectoryIndex /public/index.php

RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./public/index.php/$1 [L,QSA]
and the second one (comes with CI installation): "public/.htaccess"

# I recommend you remove `IfModule`. Because if you need mod_rewrite,
# you don't need `IfModule`. If you don't need it, you don't need this file
# at all.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|images|assets|doc|data|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Using PHP

Step 1: Change in App.php File
The first step is to open the app.app file. And the changes 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 $baseURL = 'http://localhost:8080';

To
public $baseURL = 'http://localhost/your_project_name/';
And the second change in the app.php file:

public $uriProtocol = 'REQUEST_URI';

To
public $uriProtocol = 'PATH_INFO';
Step 2: Copy index.php and .htaccess
In public/ directory, copy index.php and .htaccess to your root project directory.

Step 3: Change In index.php
In the root project directory, open index.php and edit the following line:
$pathsPath = FCPATH . '../app/Config/Paths.php';

change TO
$pathsPath = FCPATH . 'app/Config/Paths.php';