使用 mod_rewrite 显示另一个目录中的文件

发布于 2024-09-12 21:11:27 字数 655 浏览 1 评论 0原文

我已将应可从外部访问的项目中的所有文件移至单个 public 目录 - 这样我就可以将可访问的目录列入白名单,而不是将必须保持隐藏的目录列入黑名单。

但是,我无法强制 apache 重写此类网址:

  • www.example.com/images/flower.jpg

来获取

  • /public/images/flower .jpg

我想出的唯一合理的解决方案是以下几行:

RewriteCond public\/%{REQUEST_FILENAME} -f #if a file exists in the public dir,...
RewriteRule .* public/$0 [L]               #display it

不出所料,它不起作用,更准确地说,RewriteCond 部分,我无法让它匹配。

我完全不知所措,有人可以帮忙吗?

作为一个附带问题,如何调试 .htaccess 配置?如果我不知道问题是什么以及在哪里,我就无法解决问题。

I have moved all files from a project that should be accessible from outside to a single public directory - so that instead of blacklisting directories that must remain hidden, I could whitelist accessible ones.

However I cannot force apache to rewrite this kind of urls:

  • www.example.com/images/flower.jpg

to fetch

  • <DOCUMENT ROOT>/public/images/flower.jpg

Only rational solution I came up with was something among those lines:

RewriteCond public\/%{REQUEST_FILENAME} -f #if a file exists in the public dir,...
RewriteRule .* public/$0 [L]               #display it

Unsurprisingly, it does not work, more precisely, the RewriteCond part, I can't get it to match.

I am completely at loss, could someone help?

As a side question, how do you debug .htaccess configurations? I can't fix the problem if I don't know what and where it is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-09-19 21:11:27

如果您想使用 -f 测试现有文件,则需要提供如下的绝对文件系统路径:

RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule !^public/ public%{REQUEST_URI} [L]

否则使用 -F 通过子请求进行检查:

RewriteCond public%{REQUEST_URI} -F
RewriteRule !^public/ public%{REQUEST_URI} [L]

If you want to test for an existing file with -f, you need to provide an absolute file system path like this:

RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule !^public/ public%{REQUEST_URI} [L]

Otherwise use -F to do the check via a subrequest:

RewriteCond public%{REQUEST_URI} -F
RewriteRule !^public/ public%{REQUEST_URI} [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文