.htaccess的反向查找目录树中的脚本存在脚本、存在、目录、htaccess

2023-09-02 00:57:25 作者:海岛情话

是否有可能让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

在哪里脚本/foo/bar/foobar.php和foobar.php有:

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

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

和测试/某事在一个其他的名字得到通过的地方,例如路径/ PARAMS。

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]

在此先感谢

修改

目前我遇到了以下问题:

Currently I am running into the following problem:

我有一个叫做/page.php网页,但这个page.php有检查一定的选项例如像/网页/网页。这工作,但我添加/网页/网页?富=酒吧目前htaccess的使它网页/富=酒吧',而不是一个单独搞定。 我爆PARAMS获得/并给了我以下内容:

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)

在富=酒吧不应该存在,应该$ _GET ['富']或任何名称后,如果更多,更命名为得到。例如:

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)

目前的htaccess看到的第一个得到尽可能的'参数'的一部分。

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

EDIT2

我想才达到以下内容:

转到页/page.php与htaccess的它应该看起来像/网页/网页?后台= someColor

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

在我想获得主页的页面变量,GET背景应该工作homepage.php脚本。

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

推荐答案

试试这个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]