如何重写查询字符串:
test.php?cat1[]=18&cat1[]=687&xxx[]=5&xxx[]=3&xxx[]=1&yyy[]=6
到
test.php?cat1=18,687,5&xxx=3,1&yyy=6
请注意,参数(名称和值对)是动态生成的。
最佳答案
这是一个简短的 PHP 脚本,可以创建您想要的查询字符串。最好不要使用 mod_rewrite 来完成这部分,因为它完全超出了该范围:
<?php
$ret = "";
foreach($_GET as $key=>$val) {
if(is_array($val)) {
// Create the comma separated string
$value = $val[0];
$length = count($val);
for($i=1; $i < $length; $i++) {
$value .= ',' . $val[$i];
}
$ret .= "$key=$value&";
} else {
$ret .= "$key=$val&";
}
}
// Remove last '&'
$ret = substr($ret , 0, strlen($ret)-1);
// Redirect the browser
header('HTTP/1.1 302 Moved');
header("Location: /test.php?" . $ret);
?>
例如,如果将该脚本保存为 /rewrite.php
,则可以将这些规则包含在 .htaccess 文件中,以将包含数组的查询字符串的请求重新路由到 /rewrite。 php
:
RewriteCond %{QUERY_STRING} \[\]
RewriteRule ^test.php /rewrite.php [L,QSA]
然后 rewrite.php 脚本将重写查询字符串并使用串联的查询字符串重定向浏览器。
关于php - Apache 重写查询字符串(复选框数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4435344/