Apache .htaccess rewritebase not working for subfolder

I am currently working with an old vanilla PHP codebase and I am supposed to make a few improvements.

I am supposed to create some new APIs and but unfortunately at the moment, the logistics of introducing a framework like Laravel or Symfony are not quite simple.

So to make things something work, I have created a folder called api under the root directory and added a .htaccess file under it with the following content

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes

    RewriteEngine On
    RewriteBase /api/

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

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

and under it, I also have an index.php file, where I hope to put my endpoints with following code


use Dotenv\Dotenv;
use Psr\Http\Message\ServerRequestInterface;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Laminas\Diactoros\ResponseFactory;
use League\Route\Router;
use League\Route\Strategy\JsonStrategy;

define('BASE_DIR', dirname(__DIR__));

require BASE_DIR . '/vendor/autoload.php';

$dotenv = Dotenv::createImmutable(BASE_DIR);

$response_factory = new ResponseFactory();
$strategy = new JsonStrategy($response_factory);
/** @var Router $router */
$router = (new Router)->setStrategy($strategy);

$router->get('/', function (ServerRequestInterface $request): array {

    return [
        'name' => 'kelly',

$request = ServerRequestFactory::fromGlobals(
$response = $router->dispatch($request);

// send the response to the browser
(new SapiEmitter)->emit($response);

I am using the thephpleague/route to handle my routing and a few other packages to handle other needs.

But when I visit http://backend-demo.localhost/api/ I get the following

enter image description here

backend-demo.localhost is configured as a virtual host in my local apache setup.

My guess is the 404 status is being caused by the .htaccess configuration.

What can I do to make this work?

Thank you.

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi there,

It looks like that your .htaccess might not be having any effect. To fix that you need to set AllowOverride to All. You need to add this to your Apache Vhost:

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted

Make sure to change the /var/www with your actual document root.

Let me know how it goes! Regards, Bobby