Apache RewriteRule: it is possible to 'detect' the first and second path segment?

Posted by DaNieL on Stack Overflow See other posts from Stack Overflow or by DaNieL
Published on 2010-03-25T10:04:08Z Indexed on 2010/03/25 14:23 UTC
Read the original article Hit count: 310

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]

© Stack Overflow or respective owner

Related posts about apache

Related posts about mod-rewrite