首页 > 解决方案 > 如何覆盖 .htaccess 文件 Apache2 中的文件请求?

问题描述

# Structure
- example.com/static
-- .htaccess
-- images
--- file.png
--- file_cache.png
-- photos
--- file.jpg
--- file_cache.jpg

我想重写这条规则 GET param from URI 并返回文件。

例如:

http://example.com/static/images/file.png

(- 服务器将尝试通过链接返回文件)

但!

当用户声明 GET 参数和值等于“type=cache”时。

然后通过链接

http://example.com/static/images/file.png?type=cache

服务器返回文件

http://example.com/static/images/file_cache.png

谢谢!

标签: .htaccessapache2

解决方案


You can create a new file static/images/.htaccess and use this rule:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^type=([^&]+)$
RewriteRule ^file\.(png|jpe?g)$ file_%1.$1? [L,NC]

推荐阅读