3
I have a file. htaccess for route diversion on the server (production) and a router.php file for route diversion using PHP 7’s Built-in server.
php -S 0.0.0.0:8000 router.php
However, I would like to use the php router. also in production, with the same effect.
My server is managed with WHM and domains with Cpanel (also with PHP 7, with the same location settings).
Note that it is not just about putting the content in the index php., or transfer responsibility via . htaccess, since the route file has specific commands, such as the famous...
return false;
...to indicate that the php router. should be ignored and go straight to the URI folder.
And that doesn’t work on a common access.
Practical example (purely educational):
Project Folders/Files
1 - /api/execute.php
2 - /admin/
3 - /index.php
Routes (URI of Origin):
1 - /api/{servico}/{detalhe}
2 - /admin/
3 - /{caminho}/{qualquer}
What . htaccess looks like (production):
RewriteEngine On
RewriteRule ^api/?.*$ api/execute.php [NC,L]
RewriteRule ^admin/?(.*)$ admin/$1 [NC,L]
RewriteRule ^/?.*$ index.php
How does 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 lines of php router. work both locally and in production.
But the return false;
which aims to bypass the router and follow the path of Uri naturally, does not work online, obviously.
How to proceed?
Obs: This is an old system that still needs support. It is not worth changing the whole route system of it, since a new system is being created. So I just wanted to use the same php router. tb in production not to have to use the . htaccess custom for each domain using the system.
What exactly is the problem? There is a lot of sparse information in the question, but perhaps it is better to focus on the difficulty faced, describe what has been tried and what the problem with trying. In theory, if you make the.php router meet all requests, it should work in both situations, no?
– Bacco
@Bacco ready, I put an example to explain better
– Szag-Ot
You could explain yourself better?
– Tmc
What part, @Tmc? Please see the examples. I need to develop a.php router that works for both the built-in php and the online server, for the past example. Note that the only part that doesn’t work online are the ones that involve using "Return false"
– Szag-Ot
A few months ago I published an answer that solved this: http://answall.com/a/117757/4793 At this link it seems that AP did not understand anything. I know it may seem complex, but if you want I can explain the use that is actually quite simple and has support even for executions by command line interface (CLI).
– Daniel Omine
@Danielomine Thanks for the link. In fact, it helps a lot, but does not seem to solve. The direct links to file worked, but the direct links to folders did not. I may not have configured it correctly. Using the example I gave you, would you please show me how to configure htaccess (and the router if necessary) so that it works properly? (Especially when accessing another folder, which will contain its own rules, such as the "/admin/" of the example...). Already create an answer here, please, since if you solve, the reward is yours ;) In addition to helping others too
– Szag-Ot