Very often I see that on sites after going to a page there is no page extension in the address bar. For example, ruSO Help , the address looks exactly https://ru.stackoverflow.com/help , and not https://ru.stackoverflow.com/help.php or https://ru.stackoverflow.com/help.html . How exactly is this done? And what is this technology?

Plus, immediately a question from the same opera, but about the site's api . For example, take Vkontakte. The method call is as follows:
https://api.vk.com/method/METHOD_NAME?PARAMETERS&access_token=ACCESS_TOKEN&v=V
That is, after METHOD_NAME there is no .php , and on some other sites there is. Plus, I also met something like METHOD_NAME#... What does all this mean and how is it implemented? And what are the advantages of each?


UPD

Did, it turned out. apache server

Did as follows:

index.php - at the root, other pages in the pages folder
My problem with headers already sent by... solved as follows. I incorrectly approached the solution of the problem, in the index.php file I just need to handle redirects, and I stuffed the contents of the main page there.
In .htaccess wrote at the beginning of the following:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> 

Then in the index.php file:

 <?php $url = explode('/',strtolower(substr($_SERVER['REQUEST_URI'], 1))); switch($url[0]) { case '': { //Если пусто в адресе, то направляем на главную include 'pages/main_page.php'; break; } case 'news':{ include 'pages/news_page.php'; break; } case 'buy':{ include 'pages/my_buy_page.php'; break; } case 'somepage':{ include 'pages/somepage.php'; break; } } ?> 

Interestingly, the includes folder is at the same level as the pages , include folder in a file, for example, pages/news_page.php should be written without ../ , that is, just include ('includes/foobar.php');

Everything works, thanks @Stanislav, for the hint

    1 answer 1

    1. To implement the CNC (human-understandable URL), and this is how the clear addresses are called, you need to decide what server you have: nginx or apache.

    A) If you have apache, then it is enough to place the .htaccess file with the necessary parameters in the root of the site, while mod_rewrite must also be enabled (in the Apache config).

    .htaccess

     RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?url=$1 [L] 

    And in the code of the site you need to make a routing system, i.e. analysis of the obtained paths.

    Purely as an example I can give:

    index.php

     <?php $_GET['url'] = strtolower($_GET['url']); switch($_GET['url']) { case '': { echo 'INDEX'; break; } case 'news':{ echo 'NEWS'; break; } } 

    If you now enter http: //mysite.local in the address bar, you will receive INDEX , and if http: //mysite.local/news , you will receive NEWS .

    B) If you have nginx, then all conditions are set directly in the config for the site. For example:

     location / { index index.html index.php; try_files $uri $uri/ /index.php?$args; } 

    In this case (by the way, and for case A) you can use something like this for index.php :

     <?php $url = explode('/',strtolower(substr($_SERVER['REQUEST_URI'], 1))); switch($url[0]) { case '': { echo 'INDEX'; break; } case 'news':{ echo 'NEWS'; break; } } 
    1. To implement the API, you can think of what you want, but I would recommend sticking to the REST standard. You can search Google or Yandex for more details, for example, by the phrase "RESTful API".
    • With the apache, it is also better to take data from $_SERVER['REQUEST_URI'] , otherwise there may be errors with slashes in the address: ru.stackoverflow.com/questions/542869/… - Visman
    • Yes, I wrote it: In this case (by the way, and for case A ), you can use something like this - Stanislav
    • I have apache . How can I load another page on cases? include shows that headers already sent by ... - Herrgott
    • @Herrgott, then you have some information sent before sending headers. See extra spaces, echo output, UTF-8 with BOM. There are a lot of questions and answers here.stackoverflow.com/search?q=headers+already+sent+by - Visman