gpt4 book ai didi

Apache 仅针对未找到的文件重定向 404?

转载 作者:行者123 更新时间:2023-12-04 07:05:08 25 4
gpt4 key购买 nike

我要做的基本上是将所有请求发送到一个目录,如果文件存在,则发送它。如果没有,则从父目录发送它(假设它存在于那里)。文件很大,可能有很多,而且子目录会经常更改,因此文件系统链接不是一种很好的管理方式。是否有一些 Apache 配置方法可以做到这一点?例如

/path/file0
/path/file1
/path/sub1/fileA
/path/sub1/fileB
/path/sub1/fileC
/path/sub2/fileA
/path/sub2/fileB
/path/sub2/fileC

因此,如果请求来自 /path/sub1/fileB他们得到 /path/sub1/fileB (正常情况)。如果请求来自 /path/sub1/file0他们得到 /path/file0 (特例)。

或者也许在 PHP 中有一种方法,如果我可以让 Apache 将一个文件夹中的所有请求重定向到一个特定的 php 文件,该文件检查该文件是否存在,如果不存在则检查“向上”文件夹?

谢谢。

最佳答案

您可以使用 mod_rewrite要做到这一点:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^path/[^/]+/([^/]+)$ path/$1 [L]

此规则将重写 /path/ 的请求 foo / bar/path/ bar仅当 /path/ foo / bar不是普通文件。

关于Apache 仅针对未找到的文件重定向 404?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1223012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com