I have a .htaccess file for route deviation on the server (production) and a router.php file for route deviation using the PHP 7 Built-in server.
php -S 0.0.0.0:8000 router.php
However, I would like to use router.php also in production, with the same effect.
My server has management with WHM and CPanel domains (also with PHP 7, with the same site settings).
Note that it is not just about putting content in index.php , or transferring responsibility via .htaccess, since the route file has specific commands, such as the famous .. .
return false;
... to indicate that the router.php should be bypassed and go straight to the URI folder.
And this does not work on a common access.
Practical example (purely didactic):
Project Folders / Files
1 - /api/execute.php
2 - /admin/
3 - /index.php
Routes (Source URI):
1 - /api/{servico}/{detalhe}
2 - /admin/
3 - /{caminho}/{qualquer}
How is .htaccess (production):
RewriteEngine On
RewriteRule ^api/?.*$ api/execute.php [NC,L]
RewriteRule ^admin/?(.*)$ admin/$1 [NC,L]
RewriteRule ^/?.*$ index.php
How is the router.php (local / Built-in):
<?php
if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH))) {
return false;
}
// Inicializa serviço de controle de URI
$uri = new \Config\Uri();
// API
if (preg_match('/^api\/?.*$/', $uri->getUri())) {
require_once 'api/execute.php';
exit;
}
// ADMIN
if (preg_match('/^admin\/.*$/', $uri->getUri())) {
return false;
}
// Site
require_once "index.php";
This is just an example, but note the use of return false;
. All other router.php lines run both locally and in production.
But return false;
, which aims to ignore the router and follow uri's path naturally, does not work online, obviously.
How to proceed?
Obs : This is an old system that still needs support. It is not worth changing his entire route system, since a new system is being created. So I just wanted to use the same router.php in production so I would not have to use custom .htaccess for every domain that uses the system.