Codeigniter 4 Tutorials
- Codeigniter 4
- Application Run
- Application Structure
- Configuration files
- Errors
- Controller
- View
- Model
- How to remove index.php from URL
- Route
- CodeIgniter URLs
- Helper
- Session
- Global Functions and Constants
- Logging Information in CI4
- Error Handling
- Signin / SignUp
- Login / Register
- CRUD
- Crud 1
- Form Validation
- Data Table
- Pagination
- File Upload
- Show Data Using Ajax
- Select2 AJAX Autocomplete Search
- Curl Post
- Rest API
- Weblink
Home » Codeigniter 4 »
How to Remove Public and Index.php From URL in codeigniter 4?
Step 1: Change in App.php File for remove index.php from url
File name : app/Config/App.php
public $baseURL = 'http://localhost:8080';
To
public $baseURL = 'http://localhost/your_project_name/';
File name : index.php
public $uriProtocol = 'REQUEST_URI';
To
public $uriProtocol = 'PATH_INFO';
Step 2: Copy index.php and .htaccess
go in public directory. And copy index.php and .htaccess to codeigniter app root directory.
File name : index.php
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';
File name : index.php
Using htaccess :-
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
File name : 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>