【发布时间】:2010-03-25 10:04:08
【问题描述】:
我真的是 regexp 的新手,我不知道该怎么做。
我的目标是让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]
【问题讨论】:
标签: regex apache mod-rewrite url-rewriting