D
D
dNertyco2021-11-07 00:06:08
PHP
dNertyco, 2021-11-07 00:06:08

How can I process different URLs with one script?

How can I process different URLs with one script?
Let 's say example.com/info.php , example.com/profile/index.php and example.com/admin/panel/index.php are processed by one script example.com/scrypt.php
Is it possible to solve this issue through PHP?
How to implement it through nginx/(apache) config?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ivan Vekov, 2021-11-15
@dNertyco

Well, the simplest thing is, of course, to make a conditional construction like this in the script.php file:

<?php
if($_GET['action'] == 'profile'){
    require_once($_SERVER['DOCUMENT_ROOT'].'/profile/index.php');
} elseif ($_GET['action'] == 'info') {
    require_once($_SERVER['DOCUMENT_ROOT'].'/info.php');
}
?>

Etc. But this is actually complete game, and no one does that. But perhaps it will help you understand how routing works in general in the most clumsy version.
And so, I would read just about routing in php, look at the frameworks that implement it out of the box, and so on. It can be documented and convenient, like Laravel, or small and fast, like Slim.
In general, nginx has almost nothing to do with it. Is that in the config should be something like:
location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

I
Ipatiev, 2021-11-07
@Fockker

And of course, there is no information on this topic on the entire Internet, right?

T
TheAndrey7, 2021-11-07
@TheAndrey7

There are solutions - it's called routing.

A
AUser0, 2021-11-07
@AUser0

As a rule, administration / configuration of services (Apach and Nginx are HTTP services) are separated from simple programming (and they don’t climb into the service configs for every sneeze).
So the simplest thing is to create example.com/info.php, example.com/profile/index.php and example.com/admin/panel/index.php files with only one line

<?php require($_SERVER['DOCUMENT_ROOT']."/scrypt.php"); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question