我目前正在研究一个高级搜索功能,需要与.htaccess干净的urls一起工作。这个搜索可能有3-5个参数,这取决于用户输入的内容。所以基本上搜索字符串(在干净的URL之前)可能是这样的:
http://www.example.com?category=lorem&subcategory=ipsum&on_sale=1&featured=1&vendor=foo这些参数的顺序也可能不同,所以我不能使用标准的regex方法来清理URL。
发布于 2013-04-03 01:23:48
我解决这个问题的方法是在URL中使用关键字和术语。
我的最终URL如下所示:
http://www.example.com/search/term/lorem/category/shirts/subcategory/tank-top/color/red/featured/1在htaccess中,我查找了能指"/search“,并为随后的搜索参数制定了规则。
RewriteRule ^search/([A-Za-z0-9_-\s]+)/([A-Za-z0-9_-\s]+)/?$ http://www.example.com/?page=search&keywords[]=$1&terms[]=$2 [QSA,NC]等等。
所以基本上在PHP中,所有奇数索引都存储在$keywords数组中,所有偶数索引都存储在$terms数组中。
然后,我根据可接受的关键字列表检查所有关键字,以防止MySQL注入。
$allowable_keywords = array('term','category','subcategory','color','featured');然后,我可以通过组合关键字和术语,使用这个整洁的小搜索数组来过滤搜索结果。
$params = array_combine($keywords,$terms);
$params = array(
'term' => 'lorem',
'category' => 'shirts',
'subcategory' => 'tank-top',
'color' => 'red',
'featured' => 1
);希望这能有所帮助!
https://stackoverflow.com/questions/15766965
复制相似问题