Apache 重写到其他目录(如果那里存在文件)

标签 apache mod-rewrite url-rewriting exists

我的目录如下所示:

/assets
  /images
    /2011
    /2012
  /images-hq
    /2011
    /2012

在 html 文件中,每个图像都有一个标签,例如:

<img src="/assets/images/2012/example.jpg" />

我想检查 images-hq 文件夹中是否存在相同的图像,如果存在,我想将该图像发送给用户。

示例:

/assets/images/2012/example.jpg to /assets/images-hq/2012/example.jpg

IF/assets/images-hq/2012/example.jpg 存在,如果不存在,则提供原始文件。

最佳答案

我尝试这样做并且似乎有效:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} (.*)/[a-z]+/(.+)\.(gif|jpe?g|png)$
RewriteCond %1/images-hq/%2.%3 -f
RewriteRule (.*)/[a-z]+/(.+)\.(png|jpg|jpeg)$ %1/images-hq/%2.%3 [L]

我基于this solution

关于Apache 重写到其他目录(如果那里存在文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11262148/

相关文章:

url - 如何使用 mod_rewrite 将查询字符串转换为路径 URL?

cakephp - 如何从 url cakephp 中删除 index.php

url-rewriting - 自托管环境中的 URL 重写

c# - CodedUI 测试更改 URL

apache - 如何更正(Wicket in)Tomcat at/myapp behinds Apache proxy at/that send incorrect redirect to/myapp/xxx instead of/xxx?

Apache VirtualHost 多域多 SSL

在 "index"的情况下,PHP $_GET 无法获取 RewriteRule 的值

apache - htaccess 重写部分 url 并保留其余部分不变

apache - 如何正确设置 Apache HTTP - Tomcat 配置

java - 无法在 AWS Ubuntu 实例上运行 Cassandra