Slim and htaccess


First of all sorry for the basic question, but I couldn’t find a solution myself.

I “inherited” from a colleague a Wordpress based website (shared hosting). Just as any Wordpress-based website there are things that work thanks to the configuration of an htaccess file.

Problem is, in the same directory where I have the entire WP installation, I need to put a directory with all my Slim files.

So, this is the main directory situation:

  • www
    • WP index.php
    • Slim directory (api/)
      • public
        • Slim index.php

I want the following:

  1. my htaccess should possibly ignore the rules set for Wordpress pages when I call Slim endpoints (routes);
  2. in the address, the public/index.php part should be removed. That is: if my Slim files are under /api/, I’d like my API endpoints to be mydomain.ext/api/getbooks/14 (NOT mydomain.ext/api/public/index.php/getbooks/14);

This is my current htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.ext/$1 [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} !^/wp-(login|cron|comments-post)\.php$
RewriteRule ^wp-([_0-9a-zA-Z-]+)\.php [R=404,L]
RewriteRule ^readme.html(.*) [R=404,L]
RewriteRule ^license.txt(.*) [R=404,L]
RewriteRule ^wp-content/(?!themes/.*/core/css/custom\.css\.php$)(.*)\.php$ [R=404,L]

I tried to edit it but with no success: I tried what suggested the Slim docs and this tutorial too, but it didn’t work.

Can you help me?


I haven’t tried, but I suspect you will need to do something like this at the top.

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

# BEGIN WordPress

A far, far better solution would be to change hosting so you are not potentially exposing your PHP files. Good, nono-shared hosting can be found inexpensively. Or perhaps you can move your Slim files (aside from the front controller) outside of that www directory.