douyang5943 2013-11-22 20:50
浏览 7
已采纳

存在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.

  • 写回答

1条回答 默认 最新

  • dtnmnw3697 2013-11-22 21:44
    关注

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀