Automatic friendly URL in PHP [duplicate]

0

I want to develop a site type migre.me , it does not use GET visible method, and its URL looks like this:

http://migre.me/upR3N

I want to make a system to save the URL in MySQL, and create a URL of this type, without using GET method.

I do not want to leave it like this:

http://meusite.me/?url=upR3N

How do I leave as the first link ?

    
asked by anonymous 22.07.2016 / 18:16

1 answer

5

Your .htaccess would look like this:

DirectoryIndex index.php

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?url=$1
</IfModule>

By this I define that I will hide the index.php of the link, and the $_GET['url'] will be worth the rest of my link after index .

After that, just manipulate this variable to get the parameters of the link, in your case, as it seems to me that there will always be only one parameter, then it gets simpler, just get $_GET['url] anywhere and that's it. An example would be index.php like this:

<?php
if(isset($_GET['url']))
    echo "<h1>Param: " . $_GET['url'] . "</h1>";
else
    echo "<h1>Param Undefined!</h1>";

When you have more than one parameter, you must use a controller to retrieve the parameters, I will illustrate with a code from a micro framework that I developed during these vacations. Part of the parameter controller looks like this:

// pego os parametros
$url = isset($_GET['url']) ? $_GET['url'] : '';
// apago a variavel do get
unset($_GET['url']);
// se tiver algum parametro
if(!empty($url))
{
    // separo os parametros em um array, exemplo meusite.com/categoria/1
    // ficaria [0] = 'categoria', [1] = '1'
    $params = explode('/', $url);

    // arqui enta o MVC, pego meu controller
    $_GET['controller'] = isset($params[0]) ? $params[0] : '';

    // aqui entra parte do meu framework, verificando se há um sinônimo para esse controller
    $alias = Alias::check($_GET['controller']);

    // se achou um sinônimo
    if($alias != false)
    {
        // pega o controller e o metodo daquele sinonimo encontrado
        $_GET['controller'] = explode('/', $alias)[0];
        $_GET['method'] =  explode('/', $alias)[1];
    }
    // se nao achou um sinonimo do link
    else
    {
        // pega o metodo da URL e deleta o mesmo do array
        $_GET['method'] = isset($params[1]) ? $params[1] : '';
        unset($params[1]);
    }

    // deleta o controller do array
    unset($params[0]);

    // pega os demais parametros
    $get = array();
    foreach ($params as $value)
        array_push($get, $value);
    $_GET['params'] = $get;
}

I think that by showing a more complex example you can understand more about it.

Ps.: to make use of friendly URLs in Apache, it is necessary to enable module rewrite , some hosting already leaves it enabled, others not, in those cases it is necessary to contact support ask for the service. If it is in localhost , it can be enabled in WAMP as follows:

Or manually on the link :

Uncomment or include: (line 109, usually)

#LoadModule rewrite_module modules/mod_rewrite.so 

Looking like this:

LoadModule rewrite_module modules/mod_rewrite.so

Replace All:

AllowOverride None

by:

AllowOverride All

and so it should work ..

    
22.07.2016 / 18:28