Home » Php » How to remove public/index.php/ from url in Codeigniter 4

How to remove public/index.php/ from url in Codeigniter 4

Posted by: admin July 12, 2020 Leave a comment

Questions:

I want normal URL like www.sample.com/controller not www.sample.com/public/index.php/controller.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]

My .htaccess file

How to&Answers:

Just do

Step 1: In public/ directory, copy index.php and .htaccess to your root project directory.

Step 2: In the root project directory, open index.php and edit the following line:

index.php -> $pathsPath = FCPATH . ‘../app/Config/Paths.php’;

to

index.php => $pathsPath = FCPATH . ‘app/Config/Paths.php’;

Answer:

okay , this is how I solved this problem :
you need 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

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>

Answer:

I’ve no idea how you get this .htaccess. If you download CodeIgniter 4 from here and you can see Htaccess file is different what you added.

Since there are many changes in CodeIgniter (1,2,3) to Codeigniter 4, htaccess moved to public/ folder which will set to document root and .htaccess which place outside will not valid in CI 4


Correct path to .htaccess

public/.htaccess

Note: I just tested index.php URL issue and there was no issue like that. worked/loaded fine without index.php

Answer:

Step 1: In public/ directory, copy index.php and .htaccess to your root project directory.

Step 2: In the root project directory, open index.php and edit the following line:

change:

$pathsPath = FCPATH . ‘../app/Config/Paths.php’;

change TO

($pathsPath = FCPATH . ‘app/Config/Paths.php’;)

enter image description here

Answer:

  1. Set up Virtual hosts (if you haven’t already), but make sure to point to /public directory (xampp example):
ServerAdmin [email protected]
ServerName example.com
DocumentRoot "path-to-project/public"
<Directory "path-to-project/public">
    Order allow,deny
    Allow from all
    AllowOverride All
    Require all granted
</Directory>

  1. Do not touch any .htaccess file in default CI4 distribution
  2. Edit app/Config/App.php – $baseURL = ‘http://www.example.com/‘; $indexPage = ”;

Answer:

create a .htaccess file in /public with :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Answer:

have you tried this:

    RewriteEngine On
    RewriteBase /yoursite.com/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ public/index.php/$1 [L]

Answer:

I was able to “solve” the problem in my hostinger server in one project. After I created a new project with a newer version of codeigniter 4 I realized that I could no longer use the same solution and after hours of research I found a very simple solution.

What I did is I changed the root directory to be the public folder using the .htaccess that is located at the root folder of the project.

#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC,OR]
#RewriteCond %{HTTP_HOST} ^mydomain.com$
#RewriteCond %{REQUEST_URI} !public/
#RewriteRule (.*) /public/$1 [L]

Using that trick I could load my codeigniter 4 in a live server without any problem.
Actually, I used this to configure a codeigniter 4 project inside a subdomain.