Skip to content

Instantly share code, notes, and snippets.

@lackneets
Created January 15, 2016 03:25
Show Gist options
  • Save lackneets/f72da6e03c93f0e89a4e to your computer and use it in GitHub Desktop.
Save lackneets/f72da6e03c93f0e89a4e to your computer and use it in GitHub Desktop.
Redirect to minified version images
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# %{ENV:BASE} is current url path
# See http://stackoverflow.com/questions/21062290/set-rewritebase-to-the-current-folder-path-dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
# foo/xxx.png => foo/min/xxx-min.png if exists
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}$1min/$2-min.$3 -f
RewriteRule ^(.*\/)(.*)\.(png|jpg)$ %{ENV:BASE}$1min/$2-min.$3
# foo/xxx.png => foo/xxx-min.png if exists
# RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}$1$2-min.$3 -f
# RewriteRule ^(.*\/)(.*)\.(png|jpg)$ %{ENV:BASE}$1$2-min.$3
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment