In JSP using tomcat I can specify a directory to go to a servlet handler and use the filename as a parameter. IS there any way I can do this in PHP.
The purpose of this in this instance is I have a shop, so all products are of the form
sho开发者_运维百科p_view.php?cat=3
shop_detail.php?id=432
and although those work, and I am not trying to remove those entirely, it would be nice when providing links to a product to people to give links such as
products/bottle_brush
categories/accessories
which I could easily lookup from the database. If only I could catch the directories products/* and categories/*
You first need to store the product and category slugs along with the product in the database and make sure they are unique (if required).
Then you need to implement the requests shop_detail.php?slug=bottle_brush and shop_view.php?cat=accessories
Finally, if you are using Apache and the mod-rewrite module is activated, you can use the .htaccess file to rewrite your URLs (did not test that, refer to mod_rewrite documentation and .htaccess tutorials):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^products\/(.+)$ [NC]
RewriteRule ^(.*)$ shop_detail.php?slug=$1 [R,L]
mod_rewrite
精彩评论