Reputation: 25269
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 appends 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...
Upvotes: 0
Views: 380
Reputation: 11597
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
Upvotes: 1
Reputation: 7953
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)
Upvotes: 0