CodeIgniter Forums
404 can't find route - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: 404 can't find route (/showthread.php?tid=88479)



404 can't find route - risk - 09-14-2023

Hello,
so in the past I always set up my apache (XAMPP) root directory to point to the public folder of my project. But now I am working on several projects at the same time, so I set my DocumentRoot back to D:/xampp/htdocs/
In my project I set the base url inside the App.php like this: 
PHP Code:
public string $baseURL 'http://localhost/agb/project-root/public/'

Since my projectstructure is the following:
-agb
  -project-root
    -app
    -public
    -tests
    -vendor
    -writable
    -.env
    -builds
    -composer.json
    -composer.lock
    -phpunit.xml.dist
    -preload.php
    -spark

However I always get a 404 error when loading another page, than the homepage. The error message is
Can't find a route for 'get: agb/project-root/public/route'.
route can be replaced by any url I want, it is always the same error.

My routes inside the Routes.php are configured like this:
PHP Code:
$routes->match(['get''post'], 'route/(:any)''Controller::Method/$1');
$routes->get('route''Controller::Method'); 

Where route, Controller and Method are replaced with real Classes and methods.

The thing is: as soon as I add an index.php to my url ( e.g. http://localhost/agb/project-root/public/index.php/route ) the site is loaded successfully without any errors.

Why do I have this strange error? Could you please help me?


RE: 404 can't find route - kenjis - 09-14-2023

See https://codeigniter4.github.io/CodeIgniter4/installation/troubleshooting.html#i-have-to-include-index-php-in-my-url


RE: 404 can't find route - InsiteFX - 09-14-2023

Also (:any) is a catch all route and should be placed last, try swapping your two routes around.


RE: 404 can't find route - kenjis - 09-14-2023

Do you have http://localhost/index.php and http://localhost/.htaccess ?


RE: 404 can't find route - risk - 09-14-2023

Thanks for the help,
I just reinstalled codeigniter and copied all the files (Controllers, Models, Views, Libraries, etc.). Now it somehow works like it should.
So I don't know what the problem was, but I could solve it Smile


RE: 404 can't find route - VerayaTanaysa - 10-16-2023

The reason you are getting a 404 error when loading another page than the homepage is because your .htaccess file is not configured correctly.

To fix this, you need to add the following lines to your .htaccess file:

RewriteEngine On
RewriteBase /agb/project-root/public/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,NC,QSA]
This will tell Apache to rewrite all requests to the index.php file, which will then handle the routing.

Once you have added these lines to your .htaccess file, save the file and reload your browser. You should now be able to load all of your pages without any errors.

Additional notes:

Make sure that your .htaccess file is located in the /agb/project-root/public/ directory.
If you are using a different web server, such as Nginx, you will need to configure it to rewrite requests to the index.php file.
If you are using CodeIgniter, you may need to add the following line to your config/config.php file:
$config['index_page'] = '';
This will tell CodeIgniter to use the index.php file for routing.

I hope this helps!


RE: 404 can't find route - kenjis - 10-17-2023

@VerayaTanaysa The question is about CI4, but you answered as CI3.