基于查询字符串的htaccess重定向规则

时间:2018-06-11 07:12:47

标签: php .htaccess apache2

我在/index.php文件上运行动态调整大小服务,它将在/ media / _cache文件夹上创建处理(调整大小,优化)的文件,附加查询参数值,如:

http://mymedia.service.com/content/2018/Apr/filename.jpg W = 200安培; H = 200&安培; CC = 1

它将在以下位置创建一个文件: /media/_cache/content/2018/Apr/filename_w200_h200_cc.jpg

这里我需要一个htaccess规则来验证文件是否已经存在,然后它应该直接从服务器服务器而无需转到/index.php页面。

先谢谢。

1 个答案:

答案 0 :(得分:0)

经过长时间的努力,终于找到了办法:

#when url has width and height params only
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/media
RewriteCond %{QUERY_STRING} ^w=(\d+)&h=(\d+)$
RewriteRule ^(.*)\.(\w+)$ /media/_cache/$1_w%1_h%2.$2   [L]

#when url has width, height and cc
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/media
RewriteCond %{QUERY_STRING} ^w=(\d+)&h=(\d+)&cc=1$
RewriteRule ^(.*)\.(\w+)$ /media/_cache/$1_w%1_h%2_cc.$2    [L]

#if redirection rule not found file in cache, then it will be redirect to index.php for resizing & optimization
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*) index.php/$1