我试图创建一个。htaccess来处理一些PHP MVC请求。标准格式为:
domain.com/module/Controller/action
但是在某些情况下我需要传递GET变量,例如:
domain.com/module/Controller/action/foo/bar
我需要在PHP中接收值为bar
的var foo
domain.com/module/Controller/action/foo/bar/hello/world
同样的事情,我需要得到价值bar
和hello
价值world
的var foo
对于未定义数量的变量,规则必须相同,如下所示。
domain.com/module/Controller/action/foo/bar/hello/world/[...]/last/var
在模块、控制器和动作之后,我总是有一对var
/value
。
我使用的rewriterrule如下,它只检测模块,控制器和动作。
RewriteRule ^([a-z]+)/([A-Z]{1}[a-zA-Z]+)/([a-zA-Z]+)/?$ index.php?module=$1&controller=$2&action=$3
我不知道如何改变我应该做得到预期的行为。欢迎任何帮助
以下规则
RewriteRule ^/?([a-z]+)/([A-Z][a-zA-Z]+)/([a-zA-Z]+)((:?/[^/]+/[^/]+)*)$ /index.php?module=$1&controller=$2&action=$3&rest=$4
将生成以下$_GET
数组
Array
(
[module] => module
[controller] => Controller
[action] => action
[rest] => /hello/world
)
你可以使用PHP的爆炸方法和一些数组操作来解析[rest]
你也不需要[A-Z]{1}
,它已经匹配一个令牌(令牌或字符?),所以你可以用[A-Z]
代替它
如果你不想强加区分大小写的url,那么下面的简化规则将工作。
RewriteRule ^/?([a-z]+)/([a-z]+)/([a-z]+)((:?/[^/]+/[^/]+)*)$ /index.php?module=$1&controller=$2&action=$3&rest=$4 [NC]
更新2
((:?/[^/]+/[^/]+)*)
是抓取的 /key/value
对,可以分解为
/
匹配/
[^/]+
匹配1个或多个非/
的令牌
(...)*
查找(
和)
之间的值0或尽可能多地查找。
:?
将(...)
从捕获组转换为仅绑定组,并且不为其创建变量。
因此,实际上,所述部分尝试匹配/anyting_that_is_not_slash/anything_that_is_not_slash
的出现,如果找到,则将整个内容放入$4