Home » Php » apache – Always get 404 error in Slim Framework when omitting index.php in URL

apache – Always get 404 error in Slim Framework when omitting index.php in URL

Posted by: admin July 12, 2020 Leave a comment

Questions:

I created a test hello world Slim app following instructions here.

When I make this call I get a 404 error:

http://my_server/my_app/hello/John

In the other hand, when I make this call it works grand as I get a “Hello John” message:

http://my_server/my_app/index.php/hello/John

But, of course, I don’t want index.php in my URLs… What can be wrong?

======= EDIT =======

I forgot creating .htaccess file like this (following Slim Framework documentation, and in same directory as index.php):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Now I get this error:

/physical_path_to_my_files/index.php was not found on this server
How to&Answers:

If your htaccess file is in your /my_app directory, change your rules to:

RewriteEngine On

RewriteBase /my_app/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

If it’s in your document root, you need to append the path:

RewriteRule ^ /my_app/index.php [QSA,L]

Answer:

Can also change the .htaccess to the following: (I had a similar problem, and this solved it for me):

.htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

Answer:

if Jon Lin solution doesn’t work, means your .htaccess file not working.
you can verify that my adding a garbage line like

RewriteEngine On
RewriteBase /loop/v1/

This is garbage line

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

this will produce 503 error if .htaccess working fine, else you won’t get any error.

if you didn’t get any error , change Allow none to Allow all in Directory section of apache conf file or Httpd.conf

Answer:

This resource explains all we need to configure in order to use slim on Ubuntu (It helped me to solve my 404 issue) :

To summarise, there is two things to configure :

  1. Activate mod_rewrite a2enmod rewrite
  2. Modify the Apache
    configuration file (change AllowOverride None to AllowOverride All
    for the document root)

Don’t forget to restart apache2 after the changes : service apache2 restart

How To Install and Configure Slim Framework on Ubuntu 14.04