Apache RewriteRule:可以“检测”第一和第二路径段?
我真的是正则表达式的新手,我不知道该怎么做。
我的目标是让 RewriteRule
将请求 URL 路径“切片”为 3 部分:
example.com/foo
#should return: index.php?a=foo&b=&c=
example.com/foo/bar
#should return: index.php?a=foo&b=bar&c=
example.com/foo/bar/baz
#should return: index.php?a=foo&b=bar&c=baz
example.com/foo/bar/baz/bee
#should return: index.php?a=foo&b=bar&c=baz/bee
example.com/foo/bar/baz/bee/apple
#should return: index.php?a=foo&b=bar&c=baz/bee/apple
example.com/foo/bar/baz/bee/apple/and/whatever/else/no/limit/in/those/extra/parameters
#should return: index.php?a=foo&b=bar&c=baz/bee/apple/and/whatever/else/no/limit/in/those/extra/parameters
简而言之,URL 路径中的第一段 (foo
) 应该给出a,第二段(bar
)到b,以及c中字符串的其余部分
我写了这个但
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(([a-z0-9/]))?(([a-z0-9/]+))?(([a-z0-9]+))(.*)$ index.php?a=$1&b=$2&c=$3 [L,QSA]
</IfModule>
显然行不通,我什至不知道我想要的是否可能。
有什么建议吗?
编辑: 与教练经理一起玩后,我也让这个工作起来了:
RewriteRule ^([^/]*)?/?([^/]*)?/?(.*)?$ index.php?a=$1&b=$2&c=$3 [L,QSA]
Im really really a newbie in regexp and I can’t figure out how to do that.
My goal is to have the RewriteRule
to 'slice' the request URL path in 3 parts:
example.com/foo
#should return: index.php?a=foo&b=&c=
example.com/foo/bar
#should return: index.php?a=foo&b=bar&c=
example.com/foo/bar/baz
#should return: index.php?a=foo&b=bar&c=baz
example.com/foo/bar/baz/bee
#should return: index.php?a=foo&b=bar&c=baz/bee
example.com/foo/bar/baz/bee/apple
#should return: index.php?a=foo&b=bar&c=baz/bee/apple
example.com/foo/bar/baz/bee/apple/and/whatever/else/no/limit/in/those/extra/parameters
#should return: index.php?a=foo&b=bar&c=baz/bee/apple/and/whatever/else/no/limit/in/those/extra/parameters
In short, the first segment in the URL path (foo
) should be given to a, the second segment (bar
) to b, and the rest of the string in c
I wroted this one
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(([a-z0-9/]))?(([a-z0-9/]+))?(([a-z0-9]+))(.*)$ index.php?a=$1&b=$2&c=$3 [L,QSA]
</IfModule>
But obviously doesn’t work, and I don’t even know if what I want is possible.
Any suggestion?
EDIT:
After playing with coach manager, I got this one working too:
RewriteRule ^([^/]*)?/?([^/]*)?/?(.*)?$ index.php?a=$1&b=$2&c=$3 [L,QSA]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会使用这个规则(类似于你的):
如果你不想允许尾随斜杠,你可以使用这个规则来删除它们:
I would probably use this rule (similar to yours):
And if you don’t want to allow trailing slashes, you can use this rule to remove them:
这是怎么回事:
我注意到您的正则表达式存在以下问题:
在您的第一个组中,您只查找第一个字符 - 字符组后面的 + 号表示将与任意数量的字符匹配该组,只要至少有一个。
您只需要一组括号来匹配子表达式
?运算符(这是为了使匹配变得懒惰吗?)应该直接在重复运算符(+)之后
您的可选组 2 和 3 的语法不太正确
(?:stuff here){0,1}
表示“这里的内容" 可能出现,也可能不出现我已经在一个名为 The Regex Coach 的出色程序中对此进行了测试,该程序给出了下列的
What about this:
I noticed the following issues with your regex:
In your first group, you were only looking for the first character - the + sign after the character group states to match the group with any number of characters as long as there's at least one.
You only need one set of brackets to match the sub-expression
The ? operator (is this to make the match lazy?) should go straight after the repetition operator (+)
I also think the request URI starts with /, which was missing from your regex
Your syntax for the optional groups 2 and 3 wasn't quite correct
(?:stuff here){0,1}
means that "stuff here" may appear or it may notI've tested this in a great program called The Regex Coach which gives the following