Codeigniter 4 is the latest version of Codeigniter. If you are building a web application with Codeigniter, you can’t find any index.php file in the root directory. Instead of you need an access controller through yourwebsite.com/public/controller. In the latest version of Codeigniter changed the directory structure. While you building a professional application with Codeigniter, You need to remove the public from the URL for making clean URL. So this tutorial will help you to remove public from URL in Codeigniter 4.
Step 1: Copy Index.php and htaccess File from Public
In Codeigniter 4 you can’t find any index.php and htaccess file in root directory. That’s why root directory remains open. At first, Go to the public directory. Copy index.php and .htaccess file. Then paste it to codeigniter root directory.
Step 2: Edit Index.php File in Root Directory
Now go to Codeigniter 4 root directory. Edit index.php with code editor and change the following line.
$pathsPath = realpath(FCPATH . '../app/Config/Paths.php');
$pathsPath = realpath(FCPATH . '/app/Config/Paths.php');
That’s it. Now try access your controller without public in URL. An Example:
Old URL: http://example.com/public/blog
New URL: http://example/blog
I hope you successfully removed public URL in your Codeigniter 4 Project. If you have any question about codeigniter, Please comment below.