是否可以让htaccess运行目录,直到找到一个并保持正常get的完整性?例如:
/foo/bar/foobar/test/something?foo=bar&bar=foo
脚本在哪里是/foo/bar/foobar.php,foobar.php有:
array(2)
[foo] = bar,
[bar] = foo
和test/tsomething被传递到某个地方,例如在另一个获取名称"path/params"中。
我得到了这个,但它处理得不好:
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]
提前感谢
编辑
目前我遇到了以下问题:
我有一个名为/page.php的页面,但这个page.php会检查某些"选项",例如/page/homepage。这是有效的,但当我添加/页面/主页时?foo=bar当前的htaccess将其设为"homepage/foo=bar",而不是单独的get。我爆炸了params上/并给了我以下:
array (size=3)
0 => string 'page' (length=4)
1 => string 'homepage' (length=8)
2 => string 'foo=bar' (length=7)
foo=bar不应该在那里,应该是$_GET['fo']或任何名称,如果更多,则更多的名称会以它命名。例如:
array (size=3)
params => string 'page/homepage' (length=13)
foo => string 'bar' (length=3)
bar => string 'foo' (length=3)
当前htaccess将第一个get视为"param"的一部分。
编辑2
我想实现以下目标:
使用htaccess访问page/page.php,它应该看起来像/page/homepage?background=someColor
在homepage.php脚本中,我想将"homepage"作为页面变量,并且get后台应该可以工作。
试试这个代码:
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]