CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Bug: URI `*.php` shows `/` page

Open kenjis opened this issue 2 years ago • 1 comments

PHP Version

8.0

CodeIgniter4 Version

develop (edb894f2fde3eac146094eb3e5e0491a62d47f96)

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

macOS

Which server did you use?

cli-server (PHP built-in webserver)

Database

No response

What happened?

The following URLs show the Welcome page: http://localhost:8080/a.php http://localhost:8080/b.php http://localhost:8080/c.php http://localhost:8080/test/a.php

Steps to Reproduce

Install CI4 and run php spark serve.

Expected Output

Shows 404 page.

Anything else?

No response

kenjis avatar Jul 04 '22 21:07 kenjis

system/Commands/Server/rewrite.php

  1. If we use mod_rewrite then the value of $_SERVER['SCRIPT_NAME'] will always be /index.php
  2. Lost dependency on Config\App::$indexPage.

I think it should look something like this.

$script = 'We need to get the value of Config\App::$indexPage';
$_SERVER['SCRIPT_NAME'] = '/' . $script ?? 'index.php';

require_once $fcpath . $script; 

Because if the entry point is overridden, then the development server will not start.

iRedds avatar Jul 05 '22 03:07 iRedds

I have a question is how URL with .php work normally. Is it parsing the file and show if that file exist in public folder?

ping-yee avatar Aug 31 '22 13:08 ping-yee

Basically, I think http://localhost:8080/a.php is http://localhost:8080/index.php/a.php. So CI4 handles the request. But you can configure your web server to show if that PHP file exists in public folder.

kenjis avatar Aug 31 '22 21:08 kenjis

Oh, I got it. I found that the problem can be solved by adding this line $_SERVER['SCRIPT_NAME'] = '/index.php'; at system/Commands/Server/rewrite.php as @iRedds says.

But I'm not sure that getting the value of Config\App::$indexPage is needed and possible.

As Config\App::$indexPage doc shows:

If you are using mod_rewrite to remove the page set this variable so that it is blank.

Does this Config\App::$indexPage variable not to be used at using mod_rewrite in before design?

ping-yee avatar Sep 01 '22 10:09 ping-yee

Config\App::$indexPage is used when CI4 generates the site URLs. If you configure the web server to be accessible without index.php, you should set it ''.

kenjis avatar Sep 01 '22 11:09 kenjis

So this issue can be solved by adding this line $_SERVER['SCRIPT_NAME'] = '/index.php'; at system/Commands/Server/rewrite.php ?

I have been tested and this solution is useful.

ping-yee avatar Sep 01 '22 13:09 ping-yee

If we use mod_rewrite then the value of $_SERVER['SCRIPT_NAME'] will always be /index.php

Yes. And if we nagivate http://localhost/test/a.php, we will see:

404 - File Not Found Can't find a route for 'get: test/a.php'.

kenjis avatar Sep 15 '22 07:09 kenjis