Deploying Slim to an existing website

Ok I know whats the problem. The current directory structure will not work in real life.

In a web application / website, it is important to distinguish between the public and non-public areas.

The public/ directory serves your application and will therefore also be directly accessible by all browsers, search engines and API clients. All other folders are not public and must not be accessible online. This can be done by defining the public folder in Apache as DocumentRoot of your app / website.

I have written about this this topic in this Slim 4 Turorial.

The “solution” is quite simple:

  1. Move the file index.php file into public/ directory (maybe fix some paths to the autoloader)
  2. Move your public “assets” (js, css etc) directory into the public/ directory too
  3. In your prod env: Define the public/ path as your DocumentRoot
  4. Create a file: public/.htaccess with this content:
# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
  1. Create a second .htaccess file above the public/ directory with this content:
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

The second .htaccess file is only importand for your local development environment, but can be deployed too.