Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
15.9k views
in Technique[技术] by (71.8m points)

php - Page via $ _GET and manipulating the URL via .htacess

I have a page in PHP that shows the products in general (products.php):

<?php 
include_once("connect.php");
$query = "SELECT * FROM product";
$r_query = mysqli_query($conn, $query);
?>
<!DOCTYPE html>
<html lang="en">
    <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>Products</title>
    </head>
    <body>
        <div class="container theme-showcase" role="main">
            <div class="page-header">
                <h1>Products</h1>
            </div>
            <div class="row">
                <?php while($row_product = mysqli_fetch_assoc($r_query)){ ?>
                    <div class="col-sm-6 col-md-4">
                        <div class="thumbnail">
                            <img src="images/product.jpg" alt="...">
                            <div class="caption text-center">
                                <a href="details.php?id_product=<?php echo $row_product['id']; ?>"><h3><?php echo $row_product['name']; ?></h3></a>
                                <p><a href="#" class="btn btn-primary" role="button">Buy</a> </p>
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </div>
        </div>
        
    </body>
</html>

I want to choose a product on this page and it takes me to the specific product page, so I did it like this (detail.php):

<?php 
include_once("connect.php");
$id_product = $_GET['id_product'];
$query = "SELECT * FROM product WHERE id='$id_product'";
$r_query = mysqli_query($conn, $query);
$row_product = mysqli_fetch_assoc($r_query);
?>
<!DOCTYPE html>
<html lang="en">
    <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>Details</title>
    </head>
    <body>
        <div class="container theme-showcase" role="main">
            <div class="page-header">
                <h1><?php echo $row_product['name']; ?></h1>
            </div>
            <div>

              <!-- Nav tabs -->
              <ul class="nav nav-tabs" role="tablist">
                <li role="presentation" class="active"><a href="#detail" aria-controls="detail" role="tab" data-toggle="tab">Detail</a></li>
                <li role="presentation"><a href="#others" aria-controls="others" role="tab" data-toggle="tab">Others</a></li>
              </ul>

              <!-- Tab panes -->
              <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="detail">
                    <?php echo $row_product['detail']; ?>
                </div>
                <div role="tabpanel" class="tab-pane" id="others">
                    <?php echo $row_product['others']; ?>
                </div>
              </div>

            </div>
        </div>
    </body>
</html>

In the .htacess file I tried the following way:

    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} (www.)?sitename.com
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php
    
    RewriteRule ^(.*)$ detail.php?id=$1 [L,NC]
    
    Options -Indexes

But it didn't work!

So, the final URL of the product detail would look something like this: site.com/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 this way?

PS .: Just to be clear, the structure is as follows: www.sitename.com, www.sitename.com/product, www.sitename.com/product-name-1, ..., www.sitename.com/product-name-n

And the pages are as follows: index.php, product.php and detail.php

question from:https://stackoverflow.com/questions/65854834/page-via-get-and-manipulating-the-url-via-htacess

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In that case you may try with this:

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
    RewriteEngine On    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/d]+)/?$ /details.php?id_product=$1 [NC,L,QSA]
</IfModule>

This will simply do like: www.site.com/product-name to point at details.php?id_product , if necessary edit it. PS: You are vulnerable to SQL injections, please consider using prepared statements or PDO !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...