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

404 File not found error when running CodeIgniter on local Apache web server and virtual hosts #1391

Closed
hwiesmann opened this issue Oct 30, 2018 · 4 comments

Comments

@hwiesmann
Copy link

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://localhost/CodeIgniter4/public/

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://localhost/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.

@jim-parry
Copy link
Contributor

jim-parry commented Oct 31, 2018

If codeigniter.test is a virtual host mapped to htdocs/CodeIgniter4.public, then the base URL for that will be http://codeigniter.test, and the same would be used in your browser! You are not using the virtual hosting correctly. Your description is looking for application/Controllers/CodeIgniter4.test/Standard, naturally leading to a 404.
Virtual hosting uses a virtual host name defined in /etc/hosts (wherever that is on your platform), and with a element inside apache's conf/extra/httpd-vhosts.conf

@hwiesmann
Copy link
Author

Hi Jim-Parry,

sorry, that was a typo. I meant http://codeigniter.test.

The bug occurs whenever $uri starts and/or ends with a '/'.

@jim-parry
Copy link
Contributor

jim-parry commented Nov 1, 2018

Ok - so your virtual hosting is setup properly, and with application/Config settings

$baseURL = "http://codeigniter.test/"

and with application/Config/Routes settings

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Standard');

and your Standard controller is in the right namespace,
and 'http://codeigniter.test' gives a 404?
Do the Apache access_log or error_log shed any light on things?
Do you see an URL anywhere with "//"?

The setDirectory method you mention adds a trailing slash to the original one with any trailing slash removed, i.e. it ensures that there is a single trailing slash at the end of the directory name containing the controller . This sounds to me like it is specifically trying to avoid the situation you describe.

You mention $uri, which is used in several of the methods inside Router, but not setDirectory?

@jim-parry jim-parry reopened this Nov 1, 2018
@jim-parry
Copy link
Contributor

Oops - I see you have opened a different problem report :-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants