Codeigniter 4 – Removing “public” and “index.php” with Example
Removing "public" from URL
- Move the contents of the "public" folder to the root directory.
- Update the base_url in app/Config/App.php to remove "public".
Example:
public $baseURL = '(link unavailable)';
- Update the index.php file to remove the "public" directory.
Example:
require __DIR__ . '/vendor/autoload.php';
$app = new \CodeIgniter\App();
$app->run();
Removing "index.php" from URL
- Enable mod_rewrite in Apache.
- Create a new file named .htaccess in the root directory.
Example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
- Update the uri_protocol in app/Config/App.php to use the REQUEST_URI.
Example:
public $uriProtocol = 'REQUEST_URI';
- Update the index.php file to remove the index.php from the URL.
Example:
require __DIR__ . '/vendor/autoload.php';
$app = new \CodeIgniter\App();
$app->run();
I hope this helps! Let me know if you have any further questions or need more information.
Note: Removing "public" and "index.php" from the URL in CodeIgniter 4 provides a cleaner and more readable URL structure. It helps to improve the overall user experience and SEO of your application.