Question

Apache .htaccess rewritebase not working for subfolder

Posted March 22, 2021 1k views
ApachePHP

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
    </IfModule>

    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]
</IfModule>

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

<?php

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);
$dotenv->load();

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

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

    return [
        'name' => 'kelly',
    ];
});

$request = ServerRequestFactory::fromGlobals(
    $_SERVER,
    $_GET,
    $_POST,
    $_COOKIE,
    $_FILES
);
$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.

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.

×
Submit an Answer
1 answer

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
</Directory>

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

Let me know how it goes!
Regards,
Bobby

  • Hello Bobby,

    Thank you for your response but I later discovered that it was an issue with the routing library.

    • Hi there,

      No problem, happy to hear that you’ve got it working! Would you mind sharing how you’ve got the problem with the routing library sorted out here with the community?

      Regards,
      Bobby