17

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

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
caveboy
  • 231
  • 1
  • 3
  • 7

16 Answers16

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

manish
  • 423
  • 3
  • 9
  • Thanks, this is the solution. – Riyad Khalifeh Jan 02 '20 at 11:44
  • 1
    is this really safe? – Saud Khan Mar 08 '20 at 02:16
  • 4
    Its not recommended. You can rename public to be public_html. The files in public are the "public" files and the rest are not. – TimBrownlaw Apr 03 '20 at 19:44
  • 6
    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. – Raymond May 13 '20 at 18:42
  • this answer is not safe you can see that your .env file is easily accessed by http://yourdomain.tdl/.env best way also protect .env file with help of tag inside .htaccess . – swapnil akolkar Feb 11 '22 at 12:22
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>
Nassim
  • 2,879
  • 2
  • 37
  • 39
  • Apart from .htaccess file what else you need in the root directory? – arun kumar Mar 12 '20 at 13:23
  • sorry @arunkumar , was so busy to reply , that's all nothing else – Nassim Apr 03 '20 at 06:48
  • @HeribertoJuárez Great ! – Nassim Apr 28 '20 at 09:40
  • 1
    The first .htaccess file (the one in the root dir) wasn't working for me, so I used the one from this other answer: https://stackoverflow.com/questions/66592111/remove-ci4-public-index-php-with-htaccess-rewrite-rule-on-subdomain-folder. The second one I used as you sugested and everything works now :) – Diego Stéfano Sep 09 '22 at 21:33
11

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

works like charm

4

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

require_once './public/index.php';

That's it.

mogueya
  • 41
  • 3
  • Does not works wen using multiple controllers. `pages` would be located at `public/pages` https://codeigniter.com/user_guide/tutorial/static_pages.html – Aldo Jul 15 '21 at 10:25
2
  1. Set up Virtual hosts (if you haven't already), but make sure to point to /public directory (xampp example):

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>

  1. Do not touch any .htaccess file in default CI4 distribution
  2. Edit app/Config/App.php - $baseURL = 'http://www.example.com/'; $indexPage = '';
Tsefo
  • 409
  • 4
  • 15
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

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
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

lava
  • 6,020
  • 2
  • 31
  • 28
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!!!

Tabassam Ali
  • 232
  • 4
  • 12
Umair Hanif
  • 144
  • 1
  • 2
0

create a .htaccess file in /public with :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
user3153521
  • 3
  • 1
  • 1
  • 3
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

rohit mali
  • 29
  • 4
0

have you tried this:

    RewriteEngine On
    RewriteBase /yoursite.com/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ public/index.php/$1 [L]
lothux1987
  • 140
  • 2
  • 14
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.

Herii
  • 422
  • 5
  • 23
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';
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

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

woody180
  • 41
  • 7
  • This solution [has already been proposed before](https://stackoverflow.com/a/66140441/5820258). Stay vigilant when posting answers to old questions... – Lex Lustor Aug 24 '21 at 09:28
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();
Krina Mangukiya
  • 366
  • 2
  • 7
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.

heySushil
  • 493
  • 7
  • 13