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

前端 未结 9 880
有刺的猬
有刺的猬 2021-02-02 18:00

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

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCo         


        
相关标签:
9条回答
  • 2021-02-02 18:21

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

    0 讨论(0)
  • 2021-02-02 18:21

    The first step is to open the app.app file. And the changes mentioned below. You have to do this in your App.php file. remove index.php from url Codeigniter.

    So go to project_name/app/Config/App.php and change mention below: How To Remove Public/index.php/ From URL In Codeigniter 4

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

    And the second change in the app.php file:

    public $uriProtocol = 'REQUEST_URI';
    To
    public $uriProtocol = 'PATH_INFO';
    

    Step: 2

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

    We can also remove index.php in Codeigniter using .htaccess. I have given the example below.

    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    
    0 讨论(0)
  • 2021-02-02 18:22
    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 = '';
    0 讨论(0)
  • 2021-02-02 18:23

    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>
    
    0 讨论(0)
  • 2021-02-02 18:27

    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

    0 讨论(0)
  • 2021-02-02 18:28

    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.

    0 讨论(0)
提交回复
热议问题