Normally I separate the URL into two parts, the first one being the url of the site and the second all the parameters, after that I use a file of dispenser
, whose function is to separate the parameters received by the link and transoform them in 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, since it is not dynamic it 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 path to your login screen, which would in this case be: Sistema/Login.php
Example:
-.htaccess
----login
-------index.php
With this folder structure, where .htaccess is the same as above, index.php could be:
<?php
echo "Olá, ".$_GET['nome'];
When accessing:
link (in my case)
It would appear on the screen:
Hello, Overflow