开发者

Apache mod_rewrite for component / action / id URL scheme

开发者 https://www.devze.com 2023-03-26 15:43 出处:网络
I got a component-based syst开发者_Go百科em that I\'m trying to do some routing for using mod_rewrite. For some reason, I can\'t get the syntax 100% right for all cases to work properly. The following

I got a component-based syst开发者_Go百科em that I'm trying to do some routing for using mod_rewrite. For some reason, I can't get the syntax 100% right for all cases to work properly. The following are the four test cases (I can only get the last one to work):

/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

These are the three rules I have so far:

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]

The URLs should be rewritten as specified above e.g. /index.php?c=$1&a=$2&id=$3, where "c" is the component, "a" is the action and "id" is the 24 character long id. Note that there are three variants of the URL in this scheme.

Any help would be appreciated -- I'm stuck!


Got it figured out after some trial & error:

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]
0

精彩评论

暂无评论...
验证码 换一张
取 消