i want to rewrite a rule for my products.
i want to use the id and name in the url separated by a dash like this:
123-name means product id = 123 and name = name
so in my php i can get the $_GET[id] and then query my database using this id like this:
$sql = "SELECT * from Products Where productid = " . $_GET[id];
here's what i have:
RewriteEngine On
RewriteRule ^products/([0-9+])\-([a-z]+)/?$ products.php?id=$2 [NC,L]
but when i put this as url, i get a 404
why?
Source: Tips4all
First: you have a syntax error. ([0-9+]) will look for 123+asd. To fix this you have to move the + sign before the ] like: ([0-9]+).
ReplyDeleteSecond: You are using $2 in your item which is the product name. If you want to use the ID, you have to use $1.
Here's what you need to use:
RewriteEngine On
RewriteRule ^products/([0-9]+)\-([a-z0-9_\-]+)/?$ products.php?product_id=$1 [NC,L,QSA]
I added in the product numbers, dash and underscore in case you need it someday.
Third:
You should be aware of sql injections, your script is not safe. You can fix this by using mysql_real_escape_string.
I think you should use products.php?id=$1 because the first argument matched is the product id.
ReplyDelete