PHP / HTACCESS - Reading the URL as a string

0

I would like to know how I can do that by entering a URL like the following

link

Read the URL and you can do something specific with the word profile , user-1 , and news , for example, show a news page based on the user.

  

But without the profile folder, nor user-1 , nor    news .

In my opinion, it is as if the URL were read by PHP as a String, but not as directories or files. I have seen this methodology in Laravel, in the Route Class.

I'm sure there should be a "homemade" way to do it.

    
asked by Máxima Alekz 28.07.2017 в 17:56
source

2 answers

1

First, it is to enable the MOD_REWRITE apache module and create an .htaccess file at the root of the site:

Options +FollowSymlinks

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]

This will redirect all non-existent file / directory requests to index.php where you will get the URL and interpret it:

$request_url = $_SERVER['REQUEST_URI']; // perfil/usuario-1/noticias
// Separas cada parámetro
$params = explode('/', $request_url);
// $params[0] - perfil
// $params[1] - usuario-1
// $params[2] - noticias

You analyze each item of $ params to determine the actions to be taken.

    
answered by 28.07.2017 / 18:23
source
0

I would like to work with the main url and any data you want to validate in the application, handle it with parameters step.

the url would be like this:

link

    
answered by 28.07.2017 в 18:02