我正在尝试在我的PHP网址中进行修改。目前,我的网址像http://localhost/classified/1-Health-and-Beauty一样,并且运作良好。现在,我想显示我的网址,例如http://localhost/classified/Health-and-Beauty-1。
这是我的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'];
$sub_categories_name = urldecode($sub_categories_name );
$sub_categories_name = str_replace(' ', '-', $sub_categories_name);
$url = '/classified/' . urlencode($sub_id) . '-' . urlencode($sub_categories_name);
?>
<a href="<?php echo $url ?>">
<div class="cat-box p-0 m-0 cat-url">
<p class="subcat-url"><?php echo $sub_categories_name;?></p>
</div>
</a>
<?php }?>
这是我的.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d+) category-classifieds.php?sub_id=$1 [L]
答案 0 :(得分:0)
php不正确。该网址仍会以ID的形式生成,且名称之前没有。修改:
$url = '/classified/' . urlencode($sub_id) . '-' . urlencode($sub_categories_name);
收件人:
$url = '/classified/' . urlencode($sub_categories_name) . '-' . urlencode($sub_id);
答案 1 :(得分:0)
您要用数字匹配URL 结尾,而不是用数字开头的URL。请参阅this page上的“字符串起始和字符串锚定结束”。
替换:
RewriteRule ^(\d+) category-classifieds.php?sub_id=$1 [L]
具有:
RewriteRule (\d+)$ category-classifieds.php?sub_id=$1 [L]
您需要相应地更改URL的生成方式。
替换:
$url = '/classified/' . urlencode($sub_id) . '-' . urlencode($sub_categories_name);
具有:
$url = '/classified/' . urlencode($sub_categories_name) . '-' . urlencode($sub_id);