Table of Contents
Inside this short well explained article, we will see about the steps i.e Codeigniter 4 Remove Public and Index.php From URL.
When we work with CodeIgniter 4 application, then by default it contains public & index.php into the URL. But URL having “public” & “index.php” is Ok when we are working at development server or at localhost. But while deploying application to production server means at Live, we need to must make user friendly and/or SEO friendly URLs.
Note*: For this article, CodeIgniter v4.0.3 or v4.0.4 setup has been considered. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.
Let’s get started.
Download & Install CodeIgniter 4 Setup
We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed. Here are the following ways to download and install CodeIgniter 4 –
- Manual Download
- Composer Installation
- Clone Github repository of CodeIgniter 4
Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.
Here is the command to install via composer –
$ composer create-project codeigniter4/appstarter codeigniter-4
Assuming you have successfully installed application into your local system.
Settings Environment Variables
When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env
Open project in terminal
$ cp env .env
Above command will create a copy of env file to .env file. Now we are ready to use environment variables.
CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.
# CI_ENVIRONMENT = production // Do it to CI_ENVIRONMENT = development
Now application is in development mode.
CodeIgniter 4 Application Configurations Changes
To remove /public/index.php from CodeIgniter 4 application URLs we need to do very simple few steps. We will see step by step inside this explained article. A well structured URL is very efficient for users as well as for SEO point of view.
We will first see to remove /public and then we will discuss about index.php.
How to Remove “public” from URL ?
When we download CodeIgniter 4 setup, it contains public folder. That public folder actually appears into URL. If you want to test, rename to something else and check URL at browser. It will not work.
Step #1 – Move Files
Move index.php & .htaccess file from /public folder to application root.
Step #2 – Update index.php
Inside index.php file we have a path defined which points to app directory. We need to update path of that file, it’s because we have moved from public folder to root directory of application.
Initially, the path code inside index.php file –
$pathsPath = realpath(FCPATH . '../app/Config/Paths.php');
We need to update path to –
$pathsPath = realpath(FCPATH . 'app/Config/Paths.php');
Step #3
We are using CodeIgniter 4, so .htaccess file already included in the application. But if in case, this file is missing then you need to copy the following give code and paste into your .htaccess file which should be placed at root.
# Disable directory browsing Options All -Indexes # ---------------------------------------------------------------------- # Rewrite engine # ---------------------------------------------------------------------- # Turning on the rewrite engine is necessary for the following rules and features. # FollowSymLinks must be enabled for this to work. <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On # If you installed CodeIgniter in a subfolder, you will need to # change the following line to match the subfolder you need. # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase # RewriteBase / # Redirect Trailing Slashes... RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] # Rewrite "www.example.com -> example.com" RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L] # Checks to see if the user is attempting to access a valid file, # such as an image or css document, if this isn't true it sends the # request to the front controller, index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] # Ensure Authorization header is passed along RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] </IfModule> <IfModule !mod_rewrite.c> # If we don't have mod_rewrite installed, all 404's # can be sent to index.php, and everything works as normal. ErrorDocument 404 index.php </IfModule> # Disable server signature start ServerSignature Off # Disable server signature end
Now, after doing all these things you will be able to access URLs without public keyword.
How to remove “index.php” from URL ?
To remove index.php from URL additionally we need some configuration from PHP settings as well.
Step #1 – Configuration Change
Open up the file /app/Config/App.php.
Search for this code –
public $indexPage = 'index.php';
Change to –
public $indexPage = '';
Step #2
We need to enable mod_rewrite which is a PHP extension. We need to enable mod_rewrite PHP extension by using following command.
$ sudo a2enmod rewrite
Next, we need to restart the server. Assuming we have Apache server, going to restart the server by following command.
$ sudo service apache2 restart
To check the extension enable or not. Following these steps –
- Create a simple .php file at localhost, let’s called it as info.php
- Open up the file and add the following code into it – <?php phpinfo(); ?>
- Back to browser and run that file. You should see the following if your extension successfully enabled.

Step #3
Open application URLs in browser without having public and index.php into the URL.
We hope this article helped you to learn i.e Codeigniter 4 Remove Public and Index.php From URL in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.
Find More on CodeIgniter 4 here
- CodeIgniter 4 Cookie Helper Tutorial
- CodeIgniter 4 CRUD Application Tutorial
- CodeIgniter 4 CRUD REST APIs Tutorial
- CodeIgniter 4 CSRF Token in AJAX Request
- Database Query in CodeIgniter 4 Tutorial
- CodeIgniter 4 Ajax Form Data Submit
- CodeIgniter 4 Form Validation Tutorial
- CodeIgniter 4 Image Upload with Form Tutorial
- Multi language in CodeIgniter 4 Tutorial
- Stripe Payment Gateway Integration in CodeIgniter 4
- CodeIgniter 4 CSRF Token Tutorial
- CodeIgniter 4 Basics Tutorial
- CodeIgniter 4 Spark CLI Commands Tutorial
- Migration in CodeIgniter 4 Tutorial
- Seeders in CodeIgniter 4 Tutorial
Hi, I am Sanjay the founder of ONLINE WEB TUTOR. I welcome you all guys here to join us. Here you can find the web development blog articles. You can add more skills in web development courses here.
I am a Web Developer, Motivator, Author & Blogger. Total experience of 7+ years in web development. I also used to take online classes including tech seminars over web development courses. We also handle our premium clients and delivered up to 50+ projects.