Friendly link with htacess

2

I made some settings in my htacess . Check it out:

  • Within my folder www I have a System folder and inside it I have a file named Login.php . I set up forever that I type 127.0.0.1 apache always calls the file Login.php
  • Follow 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 the following.

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

    I wanted to do this: 127.0.0.1/HUGO

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

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

    Does anyone know how to do this?

        
    asked by anonymous 10.10.2016 / 15:51

    1 answer

    1

    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

        
    10.10.2016 / 15:59