Normally I separate the URL into two parts, the first being the site url and the second all parameters, after that I use a file dispenser
, whose function is to separate the parameters received by the link and transform them into GET parameters:
.htaccess
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
</IfModule>
Dispenser.php
<?php
use LegionLab\Troubadour\Routes\Alias;
/**
* Resgata parametros da URL, separa controller de method, pega
* o padrao do link (site.com/controlador/metodo/parametros). Por fim resgata os demais parametros
* colocando-o em array para serem usados no controlador.
*
* Exemplo:
* URL -> site.com/pessoas/editar/51
* Resultado do script será:
* $_GET['controller'] = 'pessoas'
* $_GET['method'] = 'editar'
* $_GET['params'] = array(0 => 51)
*
*/
$url = isset($_GET['url']) ? $_GET['url'] : '';
unset($_GET['url']);
// verifica se há uma rota
if(!empty($url))
{
// separa url nas /(barras)
$params = explode('/', $url);
// Pega o parametro 0 e 1 para ser minha rota, controller e metodo
$_GET['controller'] = isset($params[0]) ? $params[0] : '';
$_GET['method'] = isset($params[1]) ? $params[1] : '';
// Apaga variaveis
unset($params[1]);
unset($params[0]);
// Array para armazenar demais parametros
$get = array();
// coloca restante dos parametros no array
foreach ($params as $value)
array_push($get, $value);
// Verifica se há mais parametros e resgata os mesmos
if(count($_GET) > 2)
{
foreach ($_GET as $key => $value)
{
if($key != "controller" and $key != "method")
{
array_push($get, $value);
unset($_GET[$key]);
}
}
}
// cria um array com os parametros
$_GET['params'] = $get;
}
In case, as it is not dynamic can work with:
DirectoryIndex login/index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ login/index.php?nome=$1
</IfModule>
Where login/index.php
is the way to your login screen, which in case would be: Sistema/Login.php
Example:
-.htaccess
----login
-------index.php
With this folder structure, where . htaccess is equal to the previous one, the index php. could be:
<?php
echo "Olá, ".$_GET['nome'];
When accessing:
https://dev.local/ht/Overflow (in my case)
Would be displayed on the screen:
Hi there, Overflow
legal and way you did, but in my case I wanted to do it in a very simple way. as this will only be used with the name variable and on the login page
– Hugo Borges
I was thinking I just wanted to change the line
DirectoryIndex Sistema/Login.php?nome=hugo
htacess. It has as?– Hugo Borges
I put in the reply
– Leonardo
VERY GOOD, THANK YOU
– Hugo Borges