使用更改的 RewriteCond 检查文件是否存在于多个位置

发布于 2024-09-03 14:07:18 字数 367 浏览 1 评论 0原文

我想使用 mod-rewrite 在两个不同的位置检查文件:请求的 URL 以及“public”目录中请求的 URL。

这是我到目前为止所得到的:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/$0 !-f  #this line isn't working
RewriteRule ^(.*)$ index.php?$1 [L]

它可以正确确定文件是否在那里,但不能正确确定文件是否位于 /public/supplied/file/path/file.extension 。我该如何测试呢?

I want to use mod-rewrite to check for a file in two separate locations: at the requested URL, and at the requested URL within the "public" directory.

Here's what I have so far:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/$0 !-f  #this line isn't working
RewriteRule ^(.*)$ index.php?$1 [L]

It can correctly determine if the file is there, but it's not correctly determining if the file is at /public/supplied/file/path/file.extension. How do I test for that?

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

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

发布评论

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

评论(1

小糖芽 2024-09-10 14:07:18

我实际上意识到问题是 public/ 不在根目录中。它与 .htaccess 位于同一目录中。有没有办法获取 .htaccess 所在的目录,以便它可以在不同位置工作,而不需要手动修改每个位置的 .htaccess?

你可以做一个子请求。 -F 标志会将相对路径视为相对于 .htaccess 所在目录的路径。因此:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond public/$0 !-F
RewriteRule ^(.*)$ index.php?$1 [L,B]

请注意,-F 意味着比 - 更大的性能损失f,因为生成的不仅仅是一个统计数据,而是一个子请求。您还应该添加 B 标志来转义反向引用。

I've actually realized the problem is that public/ is not in the root directory. It is in the same directory that .htaccess is in. Is there a way to get the directory that .htaccess is in, so this can work in different locations without requiring manual modification of the .htaccess for each?

You can do it a subrequest. The -F flag will treat relative paths as relative to the directory the .htaccess is in. So:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond public/$0 !-F
RewriteRule ^(.*)$ index.php?$1 [L,B]

Note that the -F implies a bigger performance penalty than -f, since instead of just a stat, a subrequest is generated. You should also add the B flag to escape your back reference.

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