Link friendly with htacess

Asked

Viewed 113 times

2

I made some settings in mine htacess. Check her out:

  1. Inside of my briefcase www I have a System folder and inside it I have a file called Login.php. I set up forever I type 127.0.0.1 apache always calls the archive Login.php

Follows my htacess:

    # Index do sistema
    DirectoryIndex Sistema/Login

    # Configurações do url
    <IfModule mod_rewrite.c>
        RewriteEngine On

        # Redirecionamento do Search
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule (.*) Sistema/$1 [QSA,L]

        # A menos diretório, remover barra final
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^([^/]+)/$ http://127.0.0.1/Sistema/$1 [R=301,L]
    </IfModule>

Well what I’m trying to do is this.

To access the system I type: 127.0.0.1/Login.php?nome=HUGO

I wanted to do it like this: 127.0.0.1/HUGO

And inside the archive Login.php I have a variable that would receive the name after the / . As follows:

 $nome = filter_input(INPUT_GET, 'nome',FILTER_SANITIZE_SPECIAL_CHARS);

Someone knows how to do it?

1 answer

1


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

  • I was thinking I just wanted to change the line DirectoryIndex Sistema/Login.php?nome=hugo htacess. It has as?

  • I put in the reply

  • VERY GOOD, THANK YOU

Browser other questions tagged

You are not signed in. Login or sign up in order to post.