To remove public and index.php from URL in Codeigniter 4, you need to create a .htaccess file in the public folder and add the following code in it.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
ErrorDocument 404 /index.php
How to remove index.php in codeigniter. If you want to remove public and index.php from URL in Codeigniter 4 framework. So this tutorial will help you.
In this tutorial, we would love to share with you, how to remove public and index.php from URLs in the new Codeigniter 4 frameworks.
The directory structure of the New Codeigniter 4 framework has been changed.
How to remove public/index.php/ from URL in Codeigniter 4
Follow the below-given steps and easily 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.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
Visit inside public directory. And copy index.php and .htaccess to codeigniter app root 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';
If the above solution is not work for; so you can configure your apache server; as shown below:
In the apache server, the mode rewrite is already on. But some default values need to be changed on /etc/apache2/apache2.conf
file. Following are changes,
First, find
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
And change AllowOverride None to All and save.
Then enable mode rewrite using the command(In this case already enabeled),
Then restart the server,
$ sudo /etc/init.d/apache2 restart
Conclusion
In this tutorial, you have learned how to remove the public and index.php in new Codeigniter 4 framework.
Now you can hit the below url in your browser.
example.com/hello