Use htaccess to make the most friendly urls

1

Previously I used my url in the following way

http://www.nombredelaweb.com/rubro.php?id=juguetes-magicos

I get the value of the variable like this:

$variable = trim($_GET["id"]);

Now I update and in the htaccess file add the following lines

Options +FollowSymLinks
RewriteEngine on
Rewriterule ^rubro/([a-zA-Z0-9_-]+).php rubro.php?id=$1

Format of my new url

<a href='http://www.nombredelaweb.com/rubro/juguetes-magicos.php'>Ver nombre</a>

I get the value of the variable like this:

$variable = trim($_GET["id"]);

In this way I get my url to be more friendly.

My question: It is necessary to create a folder inside my hosting with the name (item) and inside this the file (juguetes-magicos.php) since my web would be like this (www.nombredelaweb.com/ rubro / juguetes-magicos.php)

Previously it was not necessary since the variable was in the file that did exist followed by the variable and it was rubro.php? id = juguetes-magicos.

I hope you understand me. Thank you very much.

    
asked by juan pablo 18.12.2018 в 19:31
source

1 answer

1

is not necessary.

If your route in the .htaccess is:

Rewriterule ^rubro/([a-zA-Z0-9_-]+).php rubro.php?id=$1

Well, the server will interpret any URL that complies with the expression, for example:

www.tusitio.com/rubro/mi-rubro.php

as if I had access to:

www.tusitio.com/rubro.php?id=mi-rubro

Other example:

www.tusitio.com/rubro/rubro007.php < = interpret a = > www.tusitio.com/rubro.php?id=rubro007

www.tusitio.com/rubro/el-rubro-editado.php < = interpret a = > www.tusitio.com/rubro.php?id=el-rubro-editado

And to show a data depends on the last ID it would be like this:

<?php
    // Este es el archivo rublo.php que obtendrá las URL amigables

    $id = $_GET['id'];

    if( $id == 'juguetes-magicos' ){
        echo 'Estas en juguetes magicos';
    }
    elseif( $id == 'mi-rubro' ){
        echo 'Estas en mi rubro';
    }
?>

Being the URL like this:

www.nombredelaweb.com/rubro/juguetes-magicos.php

Exit:

Estas en juguetes magicos

No need to create folders. The .htaccess allows the server to interpret the friendly URLs and pass it to the PHP code so that it understands ... I hope I have helped you:)

Greetings!

    
answered by 18.12.2018 / 21:05
source