使用 php 和 .htaccess 重写动态 url

rewrite-dynamic-url using php and .htaccess

我有这个URL

/products_ab.php?id=1

我想要这个URL

/Brass-Window-Stay.html

当我点击 /products_ab.php?id=1 link 然后 URL 变为 Brass-Window-Stay.html 动态而非静态。

我也有此 ID 和名称的数据库字段。

你能举一些 hyperlink 的例子吗(如果我从数据库中获取数据,可以在 SEO 友好的 url 中显示)?我是新手请帮忙... 我访问过类似的问题,但没有得到任何答案。 一点帮助就是很大的帮助!

更新

<ul>
<?php
$rec = mysqli_query($con,"select * from category");
while ($arr = mysqli_fetch_array($rec)) {
echo "<li><a href='products.php?id=" . $arr[0] . "'>" . $arr[1] . "</a></li>";
}
?>
</ul>

就像我如何使用上面的代码生成动态 link

在 .htaccess 文件中添加以下代码

RewriteEngine on 
Options +FollowSymlinks
RewriteBase /
RewriteRule ^product\.php$ - [L]
RewriteRule ^(.*) /product.php?product= [L]

并以 product.php 为例

<?php

print_r($_GET);

现在,一旦您访问域。com/test-product。html 或域。com/test-product-1.html 或域。com/test-product-2.html等等

您将得到如下输出:

Array ( [product] => test-product.html ) 

现在您可以使用此产品变量和查询数据库来动态生成页面。

看来我在为你编码:)

检查这个很好的参考:http://www.sitepoint.com/guide-url-rewriting/