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

16

18

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

Wellfixed answered 22/1, 2018 at 6:55 Comment(0)
G
23

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';

Gam answered 10/10, 2019 at 8:38 Comment(5)
Thanks, this is the solution.Litho
is this really safe?Trews
Its not recommended. You can rename public to be public_html. The files in public are the "public" files and the rest are not.Haematogenesis
From my understanding, this goes against the entire idea of having the /public directory in the first place. The index.php file is located there for security reasons. The solution provided by Nassim worked for me.Expansion
this answer is not safe you can see that your .env file is easily accessed by yourdomain.tdl/.env best way also protect .env file with help of <Files> tag inside .htaccess .Bedel
I
17

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>
Invalidate answered 19/9, 2019 at 8:12 Comment(4)
Apart from .htaccess file what else you need in the root directory?Volans
sorry @arunkumar , was so busy to reply , that's all nothing elseInvalidate
@HeribertoJuárez Great !Invalidate
The first .htaccess file (the one in the root dir) wasn't working for me, so I used the one from this other answer: #66592611. The second one I used as you sugested and everything works now :)Stutz
A
14

go to app/Config/App.php and then public $indexPage = 'index.php'; change to public $indexPage = '';

works like charm

Alrick answered 10/2, 2021 at 16:1 Comment(2)
In fact it did! tnksSeminarian
Recommended wayDifficult
T
4

Create a file called index.php outside of the public directory and write the command

require_once './public/index.php';

That's it.

Trapshooting answered 8/2, 2021 at 19:56 Comment(1)
Does not works wen using multiple controllers. pages would be located at public/pages codeigniter.com/user_guide/tutorial/static_pages.htmlBahr
J
2
  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 = '';
Janik answered 15/11, 2019 at 11:31 Comment(0)
M
1

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

Maffei answered 7/2, 2018 at 9:26 Comment(0)
B
1

Project Structure codeigniter4


Project Structure codeigniter4

If Run project in Xamp show Like this


enter image description here


Copy public-->index.php to rootfolder-->index.php enter image description here


Now run Like this in webbrowser show this like error to solve this enter image description here


solution enter image description here

    $pathsConfig = FCPATH . '../app/Config/Paths.php';
    to
   $pathsConfig = FCPATH . 'app/Config/Paths.php';

Now run

enter image description here


create .htaccess in root folder

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

enter image description here


Now you can switch your controller name without .htaccess your url must be like this http://localhost:8080/payinlinks/index.php/login else http://localhost:8080/payinlinks/login enter image description here

Bellini answered 27/12, 2021 at 6:22 Comment(0)
B
1

Step 1:

  • Open the Your_Project_Folder/app/Config/App.php file
  • Change $baseURL = 'http://localhost/'; [ or in-case: $baseURL = 'http://localhost:8080/'; ] to $baseURL='http://localhost/Your_Project_Name';
  • Also change $indexPage = 'index.php'; to $indexPage = '';

enter image description here

Step 2:

  • Go into Your_Project_Folder/public folder

  • Copy the .htaccess and index.php files from this folder and paste them into your folder's Your_Project_Folder root (where a spark or env file exists).

enter image description here

Step 3:

  • Open the Your_Project_Folder/index.php file (we've just pasted in project's root)

  • Change, FCPATH . '../app/Config/Paths.php'; to FCPATH . 'app/Config/Paths.php';

enter image description here

All Done!!!

Badalona answered 13/8, 2022 at 13:41 Comment(0)
A
0

create a .htaccess file in /public with :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Antipathetic answered 1/9, 2018 at 0:40 Comment(0)
R
0

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

Ruff answered 15/9, 2019 at 10:12 Comment(0)
S
0

have you tried this:

    RewriteEngine On
    RewriteBase /yoursite.com/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ public/index.php/$1 [L]
Sandisandidge answered 15/9, 2019 at 10:19 Comment(0)
H
0

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.

Haydeehayden answered 4/6, 2020 at 7:37 Comment(0)
S
0

change the index.php =>

require realpath(FCPATH . 'app/Config/Paths.php') ?: FCPATH . '../app/Config/Paths.php';

to

require realpath(FCPATH . 'app/Config/Paths.php') ?: FCPATH . 'app/Config/Paths.php';
Statius answered 3/3, 2021 at 8:39 Comment(0)
I
0

Go to app/Config/App.php and find public $indexPage = 'index.php';. Remove index.php from this public variable and that's it.

Inessential answered 24/8, 2021 at 8:27 Comment(1)
This solution has already been proposed before. Stay vigilant when posting answers to old questions...Importation
R
0

To solve the above issue removing public/index.php from url then you should following those below steps:

  1. Copy the index.php and .htaccess files from the public/ directory.

  2. Add the files to the root project directory.

  3. Change your root project directory index.php file $pathsPath = realpath(FCPATH . '../app/Config/Paths.php'); into $pathsPath = realpath(FCPATH . 'app/Config/Paths.php');

Here is the full code of index.php file:

// Valid PHP Version?
$minPHPVersion = '7.2';
if (phpversion() < $minPHPVersion)
{
    die("Your PHP version must be {$minPHPVersion} or higher to run CodeIgniter. Current version: " . phpversion());
}
unset($minPHPVersion);

// Path to the front controller (this file)
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR);

// Location of the Paths config file.
// This is the line that might need to be changed, depending on your folder structure.
$pathsPath = realpath(FCPATH . 'app/Config/Paths.php');
// ^^^ Change this if you move your application folder

/*
 *---------------------------------------------------------------
 * BOOTSTRAP THE APPLICATION
 *---------------------------------------------------------------
 * This process sets up the path constants, loads and registers
 * our autoloader, along with Composer's, loads our constants
 * and fires up an environment-specific bootstrapping.
 */

// Ensure the current directory is pointing to the front controller's directory
chdir(__DIR__);

// Load our paths config file
require $pathsPath;
$paths = new Config\Paths();

// Location of the framework bootstrap file.
$app = require rtrim($paths->systemDirectory, '/ ') . '/bootstrap.php';

/*
 *---------------------------------------------------------------
 * LAUNCH THE APPLICATION
 *---------------------------------------------------------------
 * Now that everything is setup, it's time to actually fire
 * up the engines and make this app do its thang.
 */
$app->run();
Ringdove answered 1/10, 2021 at 7:1 Comment(0)
M
0

In CodeIgniter version 4.2 or above you have to follow 3 steps:

1st Step: Go to this folder

App -> config -> app.php

Update base_url with your project folder name like we do in Codeigniter 3 Like this:

public $baseURL = 'http://localhost/your_project_name/';

In case if setup ci 4 using composer then you have this type of base_url which you need to change with the above defined base_url:

public $baseURL = 'http://localhost:8080';

2nd Step: Go to public folder and copy index.php and .htaccess files (remember copy them not cut them form here) Then paste them on root directory

Note: Same as we remove index.php in Codeigniter 3 by creating .htaccess file on the root folder and paste related code on `.htaccess' file.

3rd Step: Replace root folders .htaccess file code with this:

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

Then open root folders index.php file and check line 20 or this line:

require FCPATH . '../app/Config/Paths.php';

and remove ../ from it like this:

require FCPATH . 'app/Config/Paths.php';

In case you created css js folder in public folder and connected them using base_url on views/header.php views/footer.php or views/index.php then might be it occurs problems and don't get these files because of related problems:

When we use bootstrap cdn it gives this syntax which we made local like this but because of id="bootstrap-style" this it occurs problem so remove this form this line:

<link href="<?php echo base_url();?>/public/assets/css/bootstrap.min.css" id="bootstrap-style" />

Also form other css linked file remove this type="text/css" or any id="" it occures problem.

The right way to add css link is to define rel="stylesheet" on link like this:

<link rel="stylesheet" href="<?php echo base_url();?>/public/assets/css/bootstrap.min.css" />

In my case I got bootstrap and css related problems.

Monadnock answered 19/8, 2022 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.