Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Re-opened] 404 File not found when running CodeIgniter on local Apache web server and virtual hosts #1400

Closed
hwiesmann opened this issue Nov 1, 2018 · 14 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them in progress

Comments

@hwiesmann
Copy link

Sorry, but there were indeed typos in the previous bug report (which is closed now) that have been corrected by this post.

I am running locally an Apache web server and use virtual hosts (example name: CodeIgniter4.test). The document root is set to /CodeIgniter4/public. The base URL in App.php is therefore set to:

http://CodeIgniter4.test/

A controller "Standard" is defined and in Routes.php the following default settings exist:
$routes->setDefaultController('Standard'); $routes->setDefaultMethod('index');
There are no route definitions!

Trying to call http://CodeIgniter4.test in a browser leads to 404 File not found error which is not correct.

The reason for the error can be found in the validateRequest method of class Router: the parameter $segments contains in this case two string elements of size zero (empty strings). The reason is that autoRoute is called with the URI '/'.
Line 578 of Router.php reads:
if ( ! file_exists(APPPATH . 'Controllers/' . $test . '.php') && $directory_override === false && is_dir(APPPATH . 'Controllers/' . $this->directory . ucfirst($segments[0]))
where $directory_override is false and $this->directory and $test are initially empty.

This means that for the first test is_dir(APPPATH . 'Controllers/' . $this->directory . ucfirst($segments[0]) is true and the directory is set to '/'.
Unfortunately, it seems to be that the same test is also true when $this->directory has the value '/'. In this case the parameter of is_dir is something like APPPATH.'Controllers//' and still is_dir returns true (I am using PHP 7.1.19).

So, finally $this->directory contains '//', which is of course not correct. This means that the controller's name is "App\Controllers\\Standard". And a file relating to this controller does not exist and hence the 404 File not found exception is thrown.

PS: I am using CodeIgniter 4 version Alpha2, PHP 7.1.19.
PPS: This bug exists whenever the internally used variable $uri begins and/or ends with a '/'.

@jim-parry
Copy link
Contributor

I added to tests/system/Router/RouterTest what I think to be a unit testcase following your description...

public function testAlmostEmptyURIMatchesDefaults()
{
	$router = new Router($this->collection);

	$router->handle('/');

	$this->assertEquals($this->collection->getDefaultController(), $router->controllerName());
	$this->assertEquals($this->collection->getDefaultMethod(), $router->methodName());
}

It passes. What am I missing?

@hwiesmann
Copy link
Author

Hi,

this test will also pass in my case but:

The default setting in Config/Routes.php is
$routes->setDefaultNamespace('App\Controllers');
(see repository).

So, when I modify the test to:
public function testAlmostEmptyURIMatchesDefaults()
{
$this->collection->setDefaultNameSpace('App\Controllers');
$router = new Router($this->collection);

	$router->handle('/');

	$this->assertEquals($this->collection->getDefaultController(), $router->controllerName());
	$this->assertEquals($this->collection->getDefaultMethod(),     $router->methodName());
}

I get the result

  1. CodeIgniter\Router\RouterTest::testAlmostEmptyURIMatchesDefaults
    Failed asserting that two strings are equal.
    --- Expected
    +++ Actual
    @@ @@
    -'Home'
    +'App\Controllers\\Home'

Perhaps the default setting in Config/Routes.php confused me (or CodeIgniter 4?).

@jim-parry jim-parry added bug Verified issues on the current code behavior or pull requests that will fix them in progress labels Nov 1, 2018
@jim-parry jim-parry added this to the 4.0.0-alpha milestone Nov 1, 2018
@InsiteFX
Copy link
Contributor

InsiteFX commented Nov 4, 2018

The document root should be set to the root directory where you installed CodeIgniter 4 not the public folder. You map the public folder.

I use vhosts also.

@jim-parry
Copy link
Contributor

@InsiteFX Hmm - I understand "document root" to be the mapping, i.e. pointing at the public folder within the "project root". In the above, document root should refer to the DocumentRoot directive inside the virtual hosting element, no? That points at the front ontroller path, i.e. the public folder.
I don't understand your statement :(

@jim-parry jim-parry modified the milestones: 4.0.0-alpha, 4.0.0 Nov 13, 2018
@InsiteFX
Copy link
Contributor

Hi Jim, yes in the vhost the public_html is the document root. But in phpStorm the root is were you installed CodeIgniter, but it will not see the index.php until I map it in phpStorm to the public_html folder.

`#--------------------------------------------------------------

HTTPS:

#--------------------------------------------------------------
<VirtualHost *:443>
DocumentRoot "C:/xampp/htdocs/ci3news/public_html"
ServerName ci3news.local
ServerAlias ci3news.local
SSLEngine on
SSLCertificateFile "conf/ssl.crt/server.crt"
SSLCertificateKeyFile "conf/ssl.key/server.key"
<Directory "C:/xampp/htdocs/ci3news/public_html">
Options All
AllowOverride All
Require all granted


`

@jim-parry
Copy link
Contributor

@InsiteFX Phew - we do mean the same thing then :)
But we still seem to have CI misbehaving :(

@InsiteFX
Copy link
Contributor

Hi Jim,
If they are on a windows system then they need to do the following:

  1. edit this with notepad logged in as Administrator.

C:\Windows\System32\drivers\etc\hosts

Make sure that these are not remarked out

127.0.0.1 localhost
::1 localhost

also for vhosts I set the local up like this:

127.0.0.1 cms.local

@jim-parry
Copy link
Contributor

@InsiteFX Agreed - that would be easier, setting up a domain alias for their CI VM.
It will be in the writeup I am putting together, thanks.

@TatwiraT
Copy link

TatwiraT commented Feb 5, 2019

a turn around fix, try to use htaccess ^^

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule (.*) /public/$1 [L]

@jim-parry jim-parry modified the milestones: 4.0.0, 4.0.0-beta.2 Mar 5, 2019
@atishhamte
Copy link
Contributor

atishhamte commented Mar 13, 2019

At the time of defining the virtual host, include public folder in the directory tag. So there is no need to mention public folder in htaccess file.
I have tried the same on windows, linux and mac with XAMPP and it works like a charm...:)

@atishhamte
Copy link
Contributor

Is this still open?

@InsiteFX
Copy link
Contributor

Jim,
I' am on Windows 10 Pro using XAMPP with VHOSTS and have no problems.

`#--------------------------------------------------------------

CodeIgniter 4 Testing

#--------------------------------------------------------------

#--------------------------------------------------------------

Testing CI 4.0 Dev

#--------------------------------------------------------------
<VirtualHost *:80>
DocumentRoot "\xampp\htdocs\ci4\public_html"
ServerName ci4.local
ServerAlias ci4.local
<Directory "\xampp\htdocs\ci4\public_html">
Order allow,deny
Allow from all
AllowOverride All
Require all granted

<VirtualHost *:443>
DocumentRoot "\xampp\htdocs\ci4\public_html"
ServerName ci4.local
ServerAlias ci4.local
SSLEngine on
SSLCertificateFile "conf/ssl.crt/server.crt"
SSLCertificateKeyFile "conf/ssl.key/server.key"
<Directory "\xampp\htdocs\ci4\public_html">
Options All
AllowOverride All
Require all granted

#--------------------------------------------------------------
`

@atishhamte
Copy link
Contributor

I also commented 11 days ago regarding not facing any issue on any of the OS(mac mojave, ubuntu 18.04 and Windows 10 Pro)

@jim-parry
Copy link
Contributor

It sounds like this one is fixed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them in progress
Projects
None yet
Development

No branches or pull requests

6 participants
@InsiteFX @hwiesmann @jim-parry @TatwiraT @atishhamte and others