如何在PHP中的URL中显示带有名称的ID

时间:2019-01-30 11:44:21

标签: php .htaccess

我想在我的网址中显示带有名称的ID,目前,我的网址就像 http://localhost/classified/1

但是我希望它打印像 http://localhost/classified/1/Health-and-Beauty

这是我的.htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ category-classifieds.php?sub_id=$1 [L]

这是我的php

<?php 
$categories_name = $_GET['categories_name'];
$result = "SELECT * FROM sub_categories where categories_name='$categories_name'";
$data =mysqli_query ($conn, $result);
while ($row = mysqli_fetch_array($data))
{ 
$sub_categories_name = $row['sub_categories_name'];
$sub_id = $row['sub_id'];

<a href="<?php print $sub_id;); ?>">
    <div class="cat-box p-0 m-0 cat-url">
        <p class="subcat-url"><?php echo $sub_categories_name;?></p>
    </div>
</a>
<?php }?>

我已经尝试过了

<a href="<?php print $sub_id . '/' . str_replace(' ', '-', $sub_categories_name); ?>">

,然后我得到了所需的网址,即 http://localhost/classified/1/Health-and-Beauty

但该页面未显示任何数据

请帮帮我,我受困于其中

1 个答案:

答案 0 :(得分:0)

htaccess的更改

#Exrtact int and string from url and pass to file.
RewriteRule ^(\d+)(?!\w)\/(\w*)$ category-classifieds.php?sub_id=$1&subcategory_slug=$2 [L]