Apache rewrite query string (checkbox array)

How can I rewrite the query string, for example:

test.php?cat1[]=18&cat1[]=687&xxx[]=5&xxx[]=3&xxx[]=1&yyy[]=6 

to

 test.php?cat1=18,687,5&xxx=3,1&yyy=6 

Note that parameters (name and value pairs) are generated dynamically.

+4
source share
4 answers

Here's a short php script that creates the desired query string. It’s better not to do this part with mod_rewrite, because it is just outside this scope:

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

If you save this script as /rewrite.php , for example, you can include these rules in your .htaccess file to redirect requests with query strings containing arrays to /rewrite.php :

 RewriteCond %{QUERY_STRING} \[\] RewriteRule ^test.php /rewrite.php [L,QSA] 

Then the rewrite.php script will rewrite the query string and redirect the browser using the concatenated query string.

+8
source
 if (preg_match('/[\][]/',$_SERVER['QUERY_STRING'])) { foreach ($_GET as $key => &$val) { $_GET[$key] = is_array($val) ? implode(',', $val) : $val; } header('Location: test.php?'.rawurldecode(http_build_query(array_filter($_GET)))); } 
+3
source

test.php cat1 = 18,687.5 &? Xxx = 3.1 & YYY = 6

try pasting this function before the code:

 url_parsestring2array(& $_GET); function url_parsestring2array($args) { if (empty($args) || !is_array($args) || !$args) { return; } foreach ($args as $key => $val) { $tmp = explode(',', $val); if (count($tmp) > 1) { $args[$key] = $tmp; } } } var_dump($_GET); 

will print

array (3) {["cat1"] => array (3) {[0] => string (2) "18" [1] => string (3) "687" [2] => string (1) "5"} ["xxx"] => array (2) {[0] => string (1) "3" [1] => string (1) "1"} ["yyy"] => string ( 16" }

+1
source

I found a solution for this conversion without changing the code.

In httpd.conf (in the VirtualHost section), I define a rewrite map:

 RewriteMap programmap prg:/var/www/localhost/htdocs/chg.php 

Then in .htaccess I set the following rules:

 RewriteEngine On RewriteCond %{QUERY_STRING} (.*) RewriteRule ^(script.php) $1?${programmap:%1} [L] 

$ 1 for the first "()" in a RewriteRule

% 1 stands for first "()" in RewriteCond

Then I write this script "/var/www/localhost/htdocs/chg.php" (in PHP, but maybe in C, Perl or whatelse):

 #!/usr/bin/php -f <?php $pos1 = 2; $pos2 = $pos1 + 1; $reg = '/(([a-z0-9_]+)\[\]=([^&]*))/'; while(true){ $res=array(); $buff = trim(fgets(STDIN)); if(feof(STDIN)){ break; } $r = preg_match_all($reg, $buff, $match,PREG_SET_ORDER); if($r){ foreach($match as $row){ if(!isset($res[$row[$pos1]])){ $res[$row[$pos1]] = $row[$pos1]."=".$row[$pos2]; } else { $res[$row[$pos1]] .= ",".$row[$pos2]; } } $out=join('&',$res); } else { $out=$buff; } echo "$out\n"; } 
0
source

Source: https://habr.com/ru/post/1332053/


All Articles