多个干净的URL参数,不按顺序排列

时间:2013-04-02 14:37:41

标签: php .htaccess

我目前正在开发一种需要使用.htaccess清理网址的高级搜索功能。此搜索可以具有3-5个参数,具体取决于用户输入的内容。所以基本上搜索字符串(在干净的URL之前)可能是这样的:

http://www.example.com?category=lorem&subcategory=ipsum&on_sale=1&featured=1&vendor=foo

这些参数也可以采用不同的顺序,因此我无法使用标准的正则表达式方法来清理URL。

1 个答案:

答案 0 :(得分:0)

我解决这个问题的方法是使用关键字&网址中的条款。

我的最终网址如下:

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 
);

希望这有帮助!

相关问题