伪静态规则中RewriteCond如何获取多个参数?

现有如下表单规则:
/index.php?m=search&c=index&s=1&t=1&k=关键词
想通过伪静态的形式将路径转发为
/search?s=1&t=1&k=关键词

使用如下方式只能获取一个参数值
RewriteCond %{QUERY_STRING} ^k=(.+)$
RewriteRule ^search$ /index.php?m=search&c=index...&k=%1

使用如下方式获取多个参数的话参数的顺序是固定的,一旦调换顺序就不行了
RewriteCond %{QUERY_STRING} ^k=(.+)&s=(.+)&c=(.+)$
RewriteRule ^search$ /index.php?m=search&c=index...&k=%1&s=%2&c=%3

使用如下方式的话,有安全隐患,不能限定参数的个数
RewriteRule ^search$ /index.php?m=search&c=index [L,QSA]

请教大神们有什么好的解决方案?

阅读 4.6k
1 个回答

k s c 等参数仍然作为正则匹配,如:

RewriteCond %{QUERY_STRING} ^([a-z]+)=(.+)&([a-z]+)=(.+)&([a-z]+)=(.+)$
RewriteRule ^search$ /index.php?m=search&c=index...&%1=%2&%3=%4&%5=%6
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题