apache/mod_rewrite 从定义的文件夹中返回现有文件(如果存在),否则返回默认文件

发布于 2024-10-01 12:21:23 字数 539 浏览 0 评论 0原文

我想为我的 apache 网站编写一个 .htaccess ,具有以下规则:

对于请求(HTTP GET)此网址的客户端:

http://myserver/img123.jpg

如果文件 img123.jpg 存在于我的文件夹 /var/images/ 中,服务器返回它。 如果不存在,则返回根目录中的默认文件: /imgdefault.jpg

如果文件存在于根目录中(作为默认目录),则以下规则有效,但是我想在另一个绝对文件夹中检查它。

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^img.*\.jpg$ /imgdefault.jpg

我很确定这是可能的......但我不太了解 rewritecond 规则

I'd like to write a .htaccess for my apache website, with this kind of rules :

For a client which asks (HTTP GET) this url :

http://myserver/img123.jpg

If the file img123.jpg exists in my folder /var/images/, the server returns it.
If it doesn't exist, it returns the default file which is in the root directory : /imgdefault.jpg

The following rule works if the file exists in the root directory (as the default one), but I'd like to check it in another absolute folder.

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^img.*\.jpg$ /imgdefault.jpg

I'm pretty sure it's possible .... but I don't know rewritecond rules enough

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

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

发布评论

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

评论(1

黎夕旧梦 2024-10-08 12:21:23

您需要使用正确的路径,例如:

RewriteCond %{DOCUMENT_ROOT}/var/images/$0 -s
RewriteRule ^img.*\.jpg$ var/images/$0

You need to use the correct path like:

RewriteCond %{DOCUMENT_ROOT}/var/images/$0 -s
RewriteRule ^img.*\.jpg$ var/images/$0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文