Welcome Guest, Not a member yet? Register   Sign In
404 can't find route
#1

(This post was last modified: 09-14-2023, 02:55 AM by risk.)

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....php/route ) the site is loaded successfully without any errors.

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

See https://codeigniter4.github.io/CodeIgnit...-in-my-url
Reply
#3

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 )
Reply
#4

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

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
Reply
#6

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!
Reply
#7

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




Theme © iAndrew 2016 - Forum software by © MyBB