2010-12-14 83 views
4

怎么可能到查询字符串改写等:阿帕奇重写查询字符串(复选框阵列)

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 

注意,参数(名称和值对)是动态生成的。

+1

这是绝对可能的。但我不会用mod_rewrite而是用PHP。 – Gumbo 2010-12-14 08:00:54

+0

我也有这个问题...... – 2011-11-21 19:16:44

回答

8

下面是一个简短的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脚本将改写查询字符串和重定向与串联查询字符串的浏览器。

+0

在我的上下文中,我使用的是WordPress,而您的示例中的“test.php”是一个名为“advanced-search”的页面(这是一个已被重写的漂亮的永久链接。 htaccess)位于@“http:// root/wordpress/advanced-search /”;查询显示为:“http:// root/wordpress/advanced-search/...;是否需要对脚本进行任何调整以使其正常工作?谢谢! – 2011-11-21 22:36:31

+0

查询是什么样子的?在/ advanced-search /之后没有任何东西,你可能想要改写为/ wordpress/advanced-search/to /rewrite.php(或者你想命名的任何东西),然后在rewrite.php的底部,使用'pring(get_file_contents(“http:// your-host/wordpress/advanced-search?$ ret”));'而不是用'header()' – 2011-11-21 22:45:28

+0

重定向浏览器,查询如下所示:10.0.1.4:8888 [斜线] wordpress [斜杠]高级搜索[斜线]?property_type [] =阁楼&property_type [] =住宅 – 2011-11-21 23:00:25

1

test.php的CAT1 = 18,687,5 & XXX = 3,1 & YYY = 6

试试你的代码之前插入此函数:?

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

将打印

array(3){[“cat1”] => array(3){[0] => string(2)“18”[1] => string(3) “687”[2] => 1)“5”} [“xxx”] => array(2){[0] => string(1)“3” [1] =>串(1) “1”} [ “YYY”] =>串(1) “6”}

3
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)))); 
} 
+0

考虑''/ [\] [] /'',如果你改变它,只分配给'$ _GET [$ key]';你甚至可以用'foreach($ _GET as $ key =>&$ val)'直接赋值给'$ val',这样数组就不会被复制。否则,做得好(+1)。 – PointedEars 2011-11-28 17:16:06

+0

@PointedEars:好点,TNX ;-)我也添加了一些东西去掉空val以防万一。 ;-) – 2011-11-29 00:34:57

0

我找到了一个解决方案来做这个转换而不用修改代码。

在httpd.conf(在我虚拟主机部分)我定义了一个重写地图:

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

然后在.htaccess我设置的规则如下:

RewriteEngine On 

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

$ 1机在RewriteRule中为第一个“()”

%1代表RewriteCond中的第一个“()”

然后我写这个剧本 “/var/www/localhost/htdocs/chg.php”(在PHP中,但可以在C,Perl或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"; 
}