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
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
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';
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>
go to app/Config/App.php and then public $indexPage = 'index.php'; change to public $indexPage = '';
works like charm
Create a file called index.php outside of the public directory and write the command
require_once './public/index.php';
That's it.
ServerAdmin webmaster@example.com
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>
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
Project Structure codeigniter4
If Run project in Xamp show Like this
Copy public-->index.php to rootfolder-->index.php

Now run Like this in webbrowser show this like error to solve this

$pathsConfig = FCPATH . '../app/Config/Paths.php';
to
$pathsConfig = FCPATH . 'app/Config/Paths.php';
Now run
create .htaccess in root folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
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

Your_Project_Folder/app/Config/App.php file$baseURL = 'http://localhost/'; [ or in-case: $baseURL = 'http://localhost:8080/'; ] to $baseURL='http://localhost/Your_Project_Name';$indexPage = 'index.php'; to $indexPage = '';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).
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';
All Done!!!
create a .htaccess file in /public with :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
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';)
have you tried this:
RewriteEngine On
RewriteBase /yoursite.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
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.
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';
Go to app/Config/App.php and find public $indexPage = 'index.php';.
Remove index.php from this public variable and that's it.
To solve the above issue removing public/index.php from url then you should following those below steps:
Copy the index.php and .htaccess files from the public/ directory.
Add the files to the root project directory.
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();
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.phpinCodeigniter 3by creating.htaccessfile on the root folder andpasterelated 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
cssjsfolder inpublicfolder and connected them usingbase_urlonviews/header.phpviews/footer.phporviews/index.phpthen might be it occurs problems and don't get these files because of related problems:
When we use
bootstrapcdnit gives thissyntaxwhich we made local like this but because ofid="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
csslinkedfile remove thistype="text/css"or anyid=""it occures problem.
The right way to add
csslink is to definerel="stylesheet"on link like this:
<link rel="stylesheet" href="<?php echo base_url();?>/public/assets/css/bootstrap.min.css" />
In my case I got
bootstrapandcssrelated problems.