开发者

Rewriting url SEO friendly, how to reverse back?

Given that I know how to set the .htaccess...

So my user instead of

http://www.pets.com/products.php?id=7

will see

http://www.pets.com/products/7/

But how to manage urls with no id numbers like

http://www.pets.com/products/dog-food-purina 

How can I write a general * rule to do that? Is it possible? I thought I could write a php script that appen开发者_如何学运维ds in the htaccess the rules one by one, one for each link, but it seems crazy right?

Something like

   #RewriteRule ^dog-food-purina/$  product.php?id=1 [NC,L]  
   #RewriteRule ^dog-food-otherbrand/$  product.php?id=2 [NC,L]  
   ...etc... 


Why not make an additional unique identifier in your database, so that a row of data has both a unique ID (7) and a unique "slug" (dog-food-purina)? That way you can look up rows any way you want by simply forwarding either the slug or the ID to your PHP script and handling it all there.

For example: your purina row can be as follows:
row -> id = 1
row -> name = "Purina Dog Food"
row -> description = "This is awesome stuff!"
row -> slug = "dog-food-purina"
row -> quantity = "10lbs"
row -> price = "5"

And then do something like this:

if (is_string($param) && !empty($param)) {
    $sql = "SELECT * FROM products WHERE slug = '{$param}'"
} else if (is_numeric($param) && $param > 0) {
    $sql = "SELECT * FROM products WHERE id = {$param}"
} else return false

Hope it helps


Yes, that is unnecessary. Why not forward anything after /products/ to products.php, and that script will perform the lookup as necessary? (i.e. it will detect an id, or perform a lookup based on the name)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜