存在Htaccess反向查找目录树脚本

Is it possible to let htaccess run up directories until it finds one and keep normal get's intact ? for example:

/foo/bar/foobar/test/something?foo=bar&bar=foo

Where is the script is /foo/bar/foobar.php and foobar.php has:

array(2)
[foo] = bar,
[bar] = foo

And test/something are passed somewhere for example in an other get name 'path/params'.

I got this but it doesn't handle gets nicely:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

RewriteCond %{DOCUMENT_ROOT}/$1/$2.php !-f
RewriteCond %{QUERY_STRING} ^(?:param=)?(.*)$
RewriteRule ^(.*?)/([^/]+)/?$ $1/?param=$2/%1 [L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

Thanks in advance

Edit

Currently I am running into the following problem:

I have a page that's called /page.php but this page.php has checks for certain 'options' like for example /page/homepage. This works but when I add /page/homepage?foo=bar the current htaccess makes it 'homepage/foo=bar' instead of a separate get. I explode the params get on / and gives me the following:

array (size=3)
  0 => string 'page' (length=4)
  1 => string 'homepage' (length=8)
  2 => string 'foo=bar' (length=7)

The foo=bar shouldn't be there and should be $_GET['foo'] or whatever the name is and if more, more named gets after it. for example:

array (size=3)
  params => string 'page/homepage' (length=13)
  foo => string 'bar' (length=3)
  bar => string 'foo' (length=3)

The current htaccess see the first get as part of the 'param'.

edit2

I want to achive the following:

Go to the page /page.php with htaccess it should look like /page/homepage?background=someColor

In the homepage.php script I want to get 'homepage' as page variable and the get background should work.

Try this code:

RewriteEngine on

RewriteBase /

# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# don't do anything
RewriteRule ^ - [L]

# if current ${REQUEST_URI}.php is not a file then
# forward to the parent directory of current REQUEST_URI
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php !-f
RewriteRule ^(.*?)/([^/]+)/?$ $1/?param[]=$2 [L,QSA]

# if current ${REQUEST_URI}.php is a valid file then 
# load it be removing optional trailing slash
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]