【发布时间】:2011-03-30 13:18:10
【问题描述】:
我想使用白名单,而不是黑名单 不可访问的目录(例如deny all)。基本上,我需要这个功能:
- 如果 uri 请求的文件存在于 /public 目录中,则显示该文件;
- 否则将请求路由到/public/index.php;
- 请求字符串中不需要'public'字符串:
http://site.com/flower.jpg显示来自文件系统的DOCUMENT_ROOT/public/flower.jpg文件;
示例:
目录结构:
public\
flower.jpg
index.php
data\
secret_file.crt
请求字符串和预期结果:
-
site.com/flower.jpg- 显示flower.jpg
site.com/data/secret_file.crtsite.com/public/flower.jpgsite.com/publicsite.com/data-
site.com/any/random_url- 请求被路由到 public/index.php
我现在拥有的:
(甚至在外部帮助下那个)
# the functionality described in #1 above
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule .* public%{REQUEST_URI} [L]
# I'd like to take out the following line so ALL other requests route to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* public/index.php
如果我删除
RewriteCond %{REQUEST_FILENAME} !-f
行,它开始工作了,我尝试了无数配置,阅读了modRewrite 文档,但无法弄清楚为什么这个简单的东西拒绝简单地运行。
谁能帮助我或指出正确的方向?
完整的最终解决方案供参考
RewriteEngine On
# following line stops mod_rewrite from looping because this rule has already been applied
RewriteCond %{REQUEST_URI} !^/public/index.php
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule .* /public%{REQUEST_URI} [L]
# don't apply this rule if the first rule has been applied
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule .* /public/index.php [L]
当应用程序位于子目录(如http://site.com/uk/)中时会稍微复杂一些,但这很好用。
【问题讨论】:
标签: apache .htaccess mod-rewrite