用于组件/动作/ id URL方案的Apache mod_rewrite

时间:2011-08-09 03:01:25

标签: apache mod-rewrite url-routing

我有一个基于组件的系统,我正在尝试使用mod_rewrite进行一些路由。出于某种原因,我无法100%正确地使用语法以使所有情况正常工作。以下是四个测试用例(我只能得到最后一个测试用例):

/component/action/24_char_id  should be rewritten as /index.php?c=component&a=action&id=24_char_id
/component/24_char_id  should be rewritten as /index.php?c=component&id=24_char_id
/component/action should be rewritten as /index.php?c=component&a=action
/component should be rewritten as /index.php?c=component

这是我到目前为止的三条规则:

RewriteRule ^([^/]+)/(.*)/([0-9a-z]{24})?/?$ /index.php?c=$1&a=$2&id=$3 [L,QSA]
RewriteRule ^([^/]+)([0-9a-z]{24})?/?$       /index.php?c=$1&id=$2      [L,QSA]
RewriteRule ^([^/]+)(?:/(.*))?/?$            /index.php?c=$1&a=$2       [L,QSA]

应按上述指定重写网址,例如/index.php?c=$1&a=$2&id=$3,其中“c”是组件,“a”是动作,“id”是24个字符的长id。请注意,此方案中有三种URL变体。

任何帮助都会受到赞赏 - 我被困住了!

1 个答案:

答案 0 :(得分:0)

经过一些试验后发现了它。错误:

RewriteRule ^([^/]+)/?([0-9a-z]{24})?/?$ /index.php?c=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/([a-z]+)/?([0-9a-z]{24})?/?$ /index.php?c=$1&a=$2&id=$3 [L,QSA]