Apache RewriteRule:可以“检测”第一和第二路径段?

发布于 2024-08-26 07:50:37 字数 1364 浏览 4 评论 0原文

我真的是正则表达式的新手,我不知道该怎么做。

我的目标是让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀞厅☆埖开 2024-09-02 07:50:37

我可能会使用这个规则(类似于你的):

RewriteRule ^([^/]+)?/?([^/]+)?/?(.*) index.php?a=$1&b=$2&c=$3 [L,QSA]

如果你不想允许尾随斜杠,你可以使用这个规则来删除它们:

RewriteRule (.*)/$ /$1 [L,R=301]

I would probably use this rule (similar to yours):

RewriteRule ^([^/]+)?/?([^/]+)?/?(.*) index.php?a=$1&b=$2&c=$3 [L,QSA]

And if you don’t want to allow trailing slashes, you can use this rule to remove them:

RewriteRule (.*)/$ /$1 [L,R=301]
一身仙ぐ女味 2024-09-02 07:50:37

这是怎么回事:

RewriteRule ^/([a-z0-9]+?)(?:/([a-z0-9]+?)){0,1}(?:/(.*)){0,1}$ [L,QSA]

我注意到您的正则表达式存在以下问题:

  • 在您的第一个组中,您只查找第一个字符 - 字符组后面的 + 号表示将与任意数量的字符匹配该组,只要至少有一个。

  • 您只需要一组括号来匹配子表达式

  • ?运算符(这是为了使匹配变得懒惰吗?)应该直接在重复运算符(+)之后

  • 请求 URI 以 / 开头,而您的正则表达式中缺少它

  • 您的可选组 2 和 3 的语法不太正确 (?:stuff here){0,1} 表示“这里的内容" 可能出现,也可能不出现

我已经在一个名为 The Regex Coach 的出色程序中对此进行了测试,该程序给出了下列的

/foo (or /foo/) = index.php?a=foo&b=&c=
/foo/bar (or /foo/bar/) = index.php?a=foo&b=bar&c=
/foo/bar/any/number/of/other/parameters/after/the/third/one = index.php?a=foo&b=bar&c=any/number/of/other/parameters/after/the/third/one

What about this:

RewriteRule ^/([a-z0-9]+?)(?:/([a-z0-9]+?)){0,1}(?:/(.*)){0,1}$ [L,QSA]

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 not

I've tested this in a great program called The Regex Coach which gives the following

/foo (or /foo/) = index.php?a=foo&b=&c=
/foo/bar (or /foo/bar/) = index.php?a=foo&b=bar&c=
/foo/bar/any/number/of/other/parameters/after/the/third/one = index.php?a=foo&b=bar&c=any/number/of/other/parameters/after/the/third/one
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文