404 can't find route |
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'); 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....php/route ) the site is loaded successfully without any errors. Why do I have this strange error? Could you please help me?
Also (:any) is a catch all route and should be placed last, try swapping your two routes around.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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 ![]()
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!
@VerayaTanaysa The question is about CI4, but you answered as CI3.
|
Welcome Guest, Not a member yet? Register Sign In |