重写任意数量的路径段来查询参数 [英] Rewriting an arbitrary number of path segments to query parameters

查看:26
本文介绍了重写任意数量的路径段来查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 .htaccess 规则:

I have this .htaccess rule:

RewriteRule viewshoplatest/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ /viewshoplatest.php?$1=$2&$3=$4&$5=$6&$7=$8&$9=$10&$11=$12&$13=$14&$15=$16

它应该映射这样的 URL:

It should map a URL like this:

http://www.veepiz.com/viewshoplatest/start/10/end/10/filter/0/ownerid/0/sortby/date/sortdir/DESC/cat/0/scat/0/

为此:

http://www.veepiz.com/viewshoplatest.php?start=0&end=10&filter=0&ownerid=0&sortby=date&sortdir=DESC&cat=0&scat=0

当我点击链接并打印 $_GET 变量时,我得到了这个:

When I click on link and print $_GET variables I get this:

Array ( [start] => 10 [end] => 10 [filter] => 0 [ownerid] => 0 [sortby] => start0 [start1] => start2 [start3] => start4 [start5] => start6 )

关于它为什么表现不佳的任何想法?

Any ideas as to why it’s behaving badly?

好的,我已经通过将规则重写为

Ok i have fixed this by rewriting the rule to

RewriteRule viewshoplatest/start/(.*)/end/(.*)/filter/(.*)/ownerid/(.*)/sortby/(.*)/sortdir/(.*)/cat/(.*)/scat/(.*)/$ /viewshoplatest.php?start=$1&end=$2&filter=$3&ownerid=$4&sortby=$5&sortdir=$6&cat=$7&scat=$8

推荐答案

首先:你不应该使用 .* 如果你可以更具体,就像在这种情况下 [^/]+.因为多个 .* 会导致巨大的回溯.所以:

First of all: You shouldn’t use .* if you can be more specific, like in this case [^/]+. Because multiple .* can cause immense backtracking. So:

RewriteRule ^viewshoplatest/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /viewshoplatest.php?$1=$2&$3=$4&$5=$6&$7=$8&$9=$10&$11=$12&$13=$14&$15=$16

您可以使用类似 RegexBuddy 的方法来查看这些正则表达式处理方式的差异.

You can use a took like RegexBuddy to see the difference in how these regular expressions are processed.

但由于 mod_rewrite 只允许引用前九组(参见蒂姆的回答),您可以使用迭代方法并一次处理一个参数:

But since mod_rewrite does only allow to reference the first nine groups (see Tim’s answer), you could use an iterative approach and process one parameter at a time:

RewriteRule ^viewshoplatest/([^/]+)/([^/]+)/([^/]+/[^/]+/.*)$ /viewshoplatest/$3?$1=$2 [QSA,N]
RewriteRule ^viewshoplatest/([^/]+)/([^/]+)/([^/]*)/?$ /viewshoplatest.php?$1=$2&$3 [QSA,L]

第一条规则将一次处理一个参数对(最后一对除外),将其附加到已经存在的参数对(参见 QSA 标志),然后重新开始重写过程而不增加内部参数递归计数器(参见 N 标志).然后第二条规则将重写最后一个参数对(或只是名称)并结束迭代.

The first rule will process one parameter pair at a time (except the last pair) by append it to the already existing ones (see QSA flag) and then restart the rewriting process without incrementing the internal recursion counter (see N flag). The second rule will then rewrite the last parameter pair (or just the name) and end the iteration.

但是由于使用 N 标志可能很危险,因为它会导致无限递归,您也可以使用 PHP 来解析请求的路径:

But since using the N flag might be dangerous as it can cause an infinite recursion, you could also use PHP to parse the requested path:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = implode('/', trim($_SERVER['REQUEST_URI_PATH'], '/'));
array_shift($segments); // remove path prefix "/viewshoplatest"
for ($i=0, $n=count($segments); $i<$n; ) {
    $_GET[rawurldecode($segments[$i++])] = ($i < $n) ? rawurldecode($segments[$i++]) : null;
}

现在你只需要这个规则来传递请求:

Now you just need this rule to pass the request through:

RewriteRule ^viewshoplatest(/|$) /viewshoplatest.php [L]

这篇关于重写任意数量的路径段来查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆