Automatic PHP friendly URL

Asked

Viewed 7,852 times

0

I’m looking to develop a site like migre.me, he doesn’t use method GET visible, 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 method GET.

I don’t want to leave it like this:

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

How do I leave it as the first link?

  • https://web.archive.org/web/20140220075845/http://www.phpriot.com/Articles/search-engine-urls

1 answer

6


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>

With that I define that I will hide the index.php of the link, and the $_GET['url'] will be worth all the rest of my link after the 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 so it is simpler, just take the $_GET['url] anywhere and ready. An example would be the index.php thus:

<?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 should use a controller to rescue the parameters, I will exemplify with a code from a micro framework that I developed during this vacation. Part of the parameter controller is 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 by showing a more complex example we can understand more about the subject.

Ps.: to make use of Apache-friendly URL, it is necessary to enable the module rewrite, some hosting already leaves it enabled, others do not, in such cases it is necessary to contact the support request the service. If you are in the localhost, it can be enabled on WAMP as follows:

inserir a descrição da imagem aqui

Or manually on httpd.conf:

Uncomment or include: (line 109, normally)

#LoadModule rewrite_module modules/mod_rewrite.so 

Thus remaining:

LoadModule rewrite_module modules/mod_rewrite.so

Replace all:

AllowOverride None

for:

AllowOverride All

and so it should work..

  • I used the htaccess code you sent but it didn’t work for some unknown reason. http://towerscraft.com.br/towersgo/? url=12 - http://towerscraft.com.br/towersgo/12 - this one doesn’t work - files: http://prntscr.com/bw7hg8

  • I’ll check it. The code on the index is just one echo in $_GET['url']?

  • Yes, that’s exactly it

  • I’ll give an edited answer, with an example, but it’s basically the same, I tested it here and it worked. Surely your hosting by default leaves the mod_rewrite Apache disabled, so you should contact support and check if they support, if yes, ask for the license. If you want to test in localhost I will put in response how to enable it in WAMP.

  • I have a dedicated one at OVH, I installed apache myself, know how to enable this from httpd.conf ?

  • I’ll add on the answer.

  • I did td this, and restarted apache and still not solved

Show 3 more comments

Browser other questions tagged

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