1
I have a page in PHP that shows the products in general (products.php):
<?php include_once("conexao.php");
$query = "SELECT * FROM produtos";
$result_query = mysqli_query($conn, $query);
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Produtos</title>
</head>
<body>
<div class="container theme-showcase" role="main">
<div class="page-header">
<h1>Produtos</h1>
</div>
<div class="row">
<?php while($row_produto = mysqli_fetch_assoc($result_query)){ ?>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="imagens/produto.jpg" alt="...">
<div class="caption text-center">
<a href="detalhes.php?id_produto=<?php echo $row_produto['id']; ?>"><h3><?php echo $row_produto['nome']; ?></h3></a>
<p><a href="#" class="btn btn-primary" role="button">Comprar</a> </p>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</body>
</html>
I want to choose a product on that page and it takes me to the specific product page, so I did so (detail.php):
<?php include_once("conexao.php");
$id_produto = $_GET['id_produto'];
$query = "SELECT * FROM produtos WHERE id='$id_produto'";
$result_query = mysqli_query($conn, $query);
$row_produto = mysqli_fetch_assoc($result_query);
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Detalhe</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container theme-showcase" role="main">
<div class="page-header">
<h1><?php echo $row_produto['nome']; ?></h1>
</div>
<div>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Detalhe</a></li>
<li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Avaliação</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<?php echo $row_produto['detalhe']; ?>
</div>
<div role="tabpanel" class="tab-pane" id="profile">
<?php echo $row_produto['avaliacao']; ?>
</div>
</div>
</div>
</div>
</body>
</html>
In the file . htacess I tried the following way:
RewriteRule ^/([a-z0-9-]+)/?$ /detalhes?id_produto=$1 [L,NC]
But it didn’t work!
So the final product detail URL would look something like this: site.com.br/details? id_product=1 The question is: I would like the URL to be site.com.br/product name . Is it possible to do it that way? (the case is because I’m treating a case that’s standardized like this, but I don’t know how it was done).
To make your life easier, I recommended doing
site.com.br/produtos/nome-do-produto
. Could replaceprodutos
for something else that might make more sense to you. The point is that this way you could always load the same page, because you know it’s a product view, and inside your code you can pick up TD that is in the URL after this "keyword" and do a search in the comic book. This way TB would not disturb the other routes/ pages of the site.– tvdias
Tks! I thought of it initially! As I mentioned (site.com.br/product name) I think I could only do it if I created individual pages for each product, correct?
– NoahAnderson
I don’t know if I could create site.com.br/products/product name and manipulate by . htacess to remove /products/ ...
– NoahAnderson
Everything possible (or almost everything) is. The point is that if you want to remove the
/produto/
of the route, all orders will be directed to the same page and you will have to have a control of what should be executed (eg pages that are not products). On the other hand, if you have any identifier on the route, you will know that all these requests must go to a specific page and will be treated in the same way. ex:RewriteRule ^produto/slug-do-produto$ /detalhe.php?slug=slug-do-produto
– tvdias