By Kelly Joseph
Backend Developer
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
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.
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!
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.