Problems with friendly URLs

1

In the code from PHP, in the links I am doing the following:

<h1><a href='noticias/" .  str_replace(" ","-",$articulos[$i]['titulo']) . "'>" . $articulos[$i]['titulo'] . "</a></h1>

Which originates a URL for example such that: link (the link does not include my domain, do not try to enter because that URL does not exist).

In htaccess to understand that URL, you should take the ID of that link. I am using the following code:

RewriteEngine on
RewriteRule ^private/noticias/(.+)$ private/noticias.php?ID=$1

But it returns an error page. I do not understand exactly how it works but it's what I've seen on a website where they explain how to use friendly URLs. What I do not understand is how I will be passing the ID if I am just passing the title of the news. I'm a mess with this topic.

    
asked by JetLagFox 31.05.2017 в 02:02
source

1 answer

1

I hope this helps you:

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url_origin=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ (/.*)?/index.(htm|html|php|asp)
RewriteRule ^(.*)index.(htm|html|php|asp)$ /$1 [R=301,L]

PHP

if(!isset($_GET['url_origin']) || $_GET['url_origin'] == '')
{
    $urlNueva = 'index';
}
else
{
    $_GET['url_origin'] = rawurlencode($_GET['url_origin']);
    $_GET['url_origin'] = htmlspecialchars($_GET['url_origin']);
    $pos = strrpos($_GET['url_origin'], "'");
    if ($pos === false)
    {
        $cadenaUrl = explode("%2F", $_GET['url_origin']);//%2F es este simbolo: /
        $urlNueva = $cadenaUrl[0];
    }
    else
    {
        $urlNueva = 'index';
    }
}

With this you should be able to make friendly urls, and varibale $cadenaUrl is an array that contains all the routes of the url, that is, if your url is: /private/noticias/Ya-se-conocen-los-Games-With-Gold-de-junio , you would have:

$cadenaUrl[0] => private
$cadenaUrl[1] => noticias
$cadenaUrl[2] => Ya-se-conocen-los-Games-With-Gold-de-junio
    
answered by 31.05.2017 в 06:51