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

走远了吗. 提交于 2020-04-08 10:35:09

问题


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


回答1:


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




回答2:


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




回答3:


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




回答4:


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>



回答5:


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



回答6:


create a .htaccess file in /public with :

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



回答7:


have you tried this:

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


来源:https://stackoverflow.com/questions/48376090/how-to-remove-public-index-php-from-url-in-codeigniter-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!