Send and pick GET by AMIGAVEL URL

5

I have this tag in the index

<a href="projeto">Projeto</a>

What sends me to this page

  

localhost / project.php

What I'm leaving like this

  

localhost / project

With

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php
DirectoryIndex home.php home.html index.php index.html

But now I need to pass two variables through the url, it would look like this

<a href="projeto?id=25&nome=aqui_vai_o_nome">Projeto</a>

But I need the URL to be this way

  

localhost / project / 25 / aqui_vai_o_nome

Remembering that I need to get these two data inside the PROJECT page

This is the project code.php

<!DOCTYPE html>
<html lang="pt-br">

<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <?php include('include/baseHead-2.php'); ?>
    <!-- Bootstrap CSS -->    
    <?php include('include/fonts.php'); ?>
    <?php include('include/css-2.php'); ?>
</head>

<body>
    <?php include('include/topbar-2.php'); ?>
    <div class="container-flex m-primary container-title-page">
        <div class="container py-80">
            <div><h1 class="ff-os">Titulo da pagina</h1></div>
            <div><h2 class="ff-os"><a href="../../home" class="ff-os">Home</a> / Pagina atual</h2></div>
        </div>
    </div>
    <div class="container py-100 container-*">
        <div class="row h-200px">
            <?php
                echo $_GET['id'];
            ?>
        </div>
    </div>
    <?php include('include/footer-2.php'); ?>
</body>

<?php include('include/js-2.php'); ?>

</html>

This is my .htaccess and the new project.php where you are not getting $ _GET

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([0-9A-z_-]+)$ $1.php

RewriteRule ^projeto\/([0-9A-z_-]+)\/([0-9A-z_-]+)$ projeto.php?id=$1&nome=$2

DirectoryIndex home.php home.html index.php index.html

project.php - "project / 15 / name"

<?php

    echo $_GET['id']."</br>";
    echo $_GET['nome'];

?>
    
asked by anonymous 15.03.2018 / 00:11

1 answer

1

You can do this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([0-9A-z_-]+)$ $1.php

RewriteRule ^projeto\/([0-9A-z_-]+)\/([0-9A-z_-]+)$ projeto.php?id=$1&nome=$2

DirectoryIndex home.php home.html index.php index.html

This line below:

RewriteRule ^projeto\/([0-9A-z_-]+)\/([0-9A-z_-]+)$ projeto.php?id=$1&nome=$2

Creates a different rule, just in case the URL starts with projeto .

To get the id and name, just use $_GET['id'] and $_GET['nome'] inside projeto.php

If you click:

<a href="projeto/15/nome_do_projeto">ver projeto</a>

In projeto.php you can view using echo :

   echo $_GET['id']."</br>";
   echo $_GET['nome'];

Return:

15
nome_do_projeto

EDIT: Problems accessing other files

When you use friendly url you need to put the full path of where the image, css and js files are. In your case it would look like this, an example:

<img src="http://localhost/imagem/imagemExemplo.js"/><linkrel="stylesheet" type = "text/css" href="http://localhost/res/style/estilo.css" media="screen" />

I usually create a constant and use it throughout the site:

define("URL", "http://localhost/");
<link rel='stylesheet' type = 'text/css' href='<?php echo URL; ?>/style/estilo.css' media='screen' />

This makes it easier, because when you publish the site, just change the url.

Edit 2:

Change .htaccess to:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule ^([0-9A-z_-]+)$ $1.php

RewriteRule ^projeto\/([0-9A-z_-]+)\/([0-9A-z_-]+)$ projeto.php?id=$1&nome=$2

DirectoryIndex home.php home.html index.php index.html
    
15.03.2018 / 06:17