将2个或更多查询字符串参数更改为文件夹结构

时间:2015-08-27 12:06:34

标签: regex .htaccess mod-rewrite redirect

我想改变

http://example.com/view/52/rocket

?i=

我目前有(仅限RewriteCond %{THE_REQUEST} \ /view\?i=([0-9]+) RewriteRule ^ /view/%1? [L,R] RewriteRule ^view/([0-9]+)$ /view?i=$1 [NC,L,QSA] ):

/view

另外,我如何重写以下内容(没有http://example.com/?i=52&t=rocket)?

^ possibly without the /

   header('Content-type: application/json');
    $return=array();
    $level=$_REQUEST['level'];
    $sql="SELECT * FROM guess WHERE level=$level";
    $res = $st->query($sql);
    if($res->num_rows>=1){
        $images=array();
        while($row= $res->fetch_assoc())
        {
            $images[]['id']= ($row['id']);
            $images[]['type']= ($row['type']);
            $images[]['question']= utf8_encode($row['question']);//Convert this to UTF-8 
            $images[]['answer']= ($row['answer']);
        }
        $return['list']= $images;
    }
echo json_encode($return);

3 个答案:

答案 0 :(得分:0)

您可以使用此正则表达式进行搜索:

[^\/]+=

Regex live here.

用空字符串替换匹配的文本。

解释

[^\/]     # looks for not '/' characters
+         # at least one of them together
=         # and must end in an '=' character

希望它有所帮助。

答案 1 :(得分:0)

您可以使用:

RewriteCond %{THE_REQUEST} \s/+view\?i=([0-9]+)&t=([^&\s]+)
RewriteRule ^ /view/%1/%2? [L,R]

RewriteCond %{THE_REQUEST} \s/+\?i=([0-9]+)&t=([^&\s]+)
RewriteRule ^ /%1/%2? [L,R]

RewriteCond %{THE_REQUEST} \s/+view\?i=([0-9]+)\s
RewriteRule ^ /view/%1? [L,R]

RewriteRule ^view/([0-9]+)/([^/]+)/?$ /view?i=$1&t=$2 [NC,L,QSA]

RewriteRule ^([0-9]+)/([^/]+)/?$ ?i=$1&t=$2 [L,QSA]

RewriteRule ^view/([0-9]+)$ /view?i=$1 [NC,L,QSA]

答案 2 :(得分:0)

第一个:

RewriteRule ^(.*/view)\?i=(\d*)&t=(.*) $1/$2/$3

第二个:

RewriteRule ^(.*)/view\?i=(\d*)&t=(.*) $1/$2/$3