【问题标题】:Mod Rewrite show private file access deniedMod Rewrite 显示私有文件访问被拒绝
【发布时间】:2013-04-03 14:52:13
【问题描述】:

我有以下模组重写规则:

RewriteCond %{DOCUMENT_ROOT}/../application/%{REQUEST_URI} -f
RewriteRule ([\w]+\/www\/(.*)) %{DOCUMENT_ROOT}/../../application/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php/$1

我的服务器目录布局是这样的:

/application
--/example
----/www
------/test.jpg
/www <- public root
--/.htaccess
--/index.php

基本上,这个想法是,您可以像这样访问 index.php:

example.com/test/paths/ < would pass test/paths/ to index.php

example.com/example/www/test.png < would pass example/www/test.png to index.php

example.com/example/www/test.jpg < would output the contents of test.jpg

现在前两个示例完美运行。当我们到达最后一个输出 test.jpg 内容(位于非公共目录中)的示例时,我收到此错误:

您无权访问此服务器上的 /C:/web/git/framework/application/example/www/test.jpg。

我可以将它传递给另一个公共 php 文件,然后该文件将执行私有文件的 file_get_contents 并以这种方式显示它,但理想情况下我不想产生额外的 php 进程。无论如何我可以在没有访问被拒绝部分的情况下做我想做的事吗?

编辑

我通过不同的方法解决了这个问题。我将整个应用程序目录移动到 www 目录中,现在我使用此 HT ACCESS 文件仅加载 \w/www/* 目录中的文件

RewriteCond %{REQUEST_URI} ^/[\w]+/www/(.*)$
RewriteCond %{DOCUMENT_ROOT}/applications/%{REQUEST_URI} -f
RewriteRule ([\w]+\/www\/(.*)) /applications/$1 [L]

RewriteCond %{REQUEST_URI} !^/index.php/.*
RewriteRule ^(.*)$ ./index.php/$1

【问题讨论】:

  • 为什么将应该通过 HTTP 可用的资源放在文档根目录之外?
  • 在我的框架中,您可以拥有许多“应用程序”,并且在每个应用程序中都有 MVC 文件和其他 php 文件以及一个存储所有资产的“www”文件夹。这个想法是一个应用程序只是简单的拖放,框架负责其余的工作。出于安全原因,框架文件和应用程序文件不公开,但我希望“www”文件夹可以公开访问
  • 定义一个 ALIAS 是要做的事情,我会说 - 但请注意,这只能在服务器配置中实现,而不是通过 .htaccess - httpd.apache.org/docs/2.2/mod/mod_alias.html#alias
  • 一旦有人从互联网上下载了任何文件,您应该假定它是公开的……无论它是流式传输、链接还是嵌入。
  • 我意识到这一点,但我只想公开应用程序 www 文件夹中的资产。

标签: php .htaccess mod-rewrite url-rewriting symlink


【解决方案1】:

我通过不同的方法解决了这个问题。我将整个应用程序目录移动到 www 目录中,现在我使用此 HT ACCESS 文件仅加载 \w/www/* 目录中的文件

RewriteCond %{REQUEST_URI} ^/[\w]+/www/(.*)$
RewriteCond %{DOCUMENT_ROOT}/applications/%{REQUEST_URI} -f
RewriteRule ([\w]+\/www\/(.*)) /applications/$1 [L]

RewriteCond %{REQUEST_URI} !^/index.php/.*
RewriteRule ^(.*)$ ./index.php/$1

【讨论】:

  • 两天前我不能接受自己的答案,但我明白你的意思
猜你喜欢
  • 2016-03-06
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多