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

时间:2022-11-25 11:18:41

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

有没有可能让htaccess运行目录,直到找到一个并保持正常的get完好无损?例如:

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

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

脚本在/foo/bar/foobar.php和foobar.php的位置在哪里:

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

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

并且测试/某些东西在某处传递,例如在另一个获取名称'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:

我有一个名为/page.php的页面,但是这个page.php检查了某些“选项”,例如/ page / homepage。这有效但是当我添加/ page / homepage?foo = bar时,当前的htaccess使它成为'homepage / foo = bar'而不是单独的get。我爆炸了params get /并给了我以下内容:

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:

foo = bar不应该在那里,应该是$ _GET ['foo']或者名称是什么,如果更多,更多的名字在它之后。例如:

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'.

当前的htaccess将第一个获取视为'param'的一部分。

edit2

I want to achive the following:

我想要实现以下目标:

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

转到页面/page.php与htaccess它应该看起来像/ page / homepage?background = someColor

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

在homepage.php脚本中,我希望将“主页”作为页面变量,并且获取背景应该可行。

1 个解决方案

#1


0  

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]

#1


0  

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]