This is no doubt a no but has anyone has experienced reading and redirecting the request in apache based on a config rule?


<If "!%{REQUEST_URI} =~ m#files/cache/static/[A-Za-z_\-0-9]+.cache# && -f '/var/www/files/cache/static/%{tolower:%{REQUEST_URI}}.cache'">
           #Read /files/cache/static/xyz.cache and redirect to url in contents

I need to be able to read the contents of a file and redirect to the url specified.

This would allow apache to deal with a dynamic image request instead of it going through PHP.

I'm already doing something similar with HTML pages and it works beautifully but images I want to avoid having a "cached" copy of the image and would like to redirect to the actual file without involving an additional parser.

EDIT for clarity:

User 1 visits example.com/username-128-128.jpg

PHP then parses the url and retrieves the image at the right size and scale (either generating it or retrieving from file storage)

say the actual image is /var/www/files/images/username/128/128/image.jpg

PHP creates a file name "username-128-128.jpg.cache" file in /var/www/files/cache/static which contains "example.com/files/images/username/128/128/image.jpg" and then redirects to the actual file

Header("Location: example.com/files/images/username/128/128/image.jpg");

User 2: Requests the same file example.com/username-128-128.jpg

Apache see's there is a file "/var/www/files/cache/static/username-128-128.jpg.cache" it then reads that file and uses mod_rewrite to redirect to the contents of the file.

What i'm trying to avoid is creating a copy of /var/www/files/images/username/128/128/image.jpg in /var/www/files/cache/static/

Because it's wasteful.



It looks like your logic is backward but mod_rewrite may help you:

RewriteMap lc int:tolower
RewriteMap nocache "txt:/files/cache/static/xyz.cache"
RewriteCond /var/www/files/cache/static/${lc:%{REQUEST_URI}}.cache !-f
RewriteRule files/cache/static/[A-Za-z_\-0-9]+.cache ${nocache:foo} [L]

Rather than putting just the destination URL in /files/cache/static/xyz.cache you'd have to put a map:

foo http://wwww.example.com/actual_file


foo /actual_file

foo could be a variable as well.

  • I've edited my question to make it clearer. I understand how to do the above but we're dealing with images. I don't want to make another copy of an image just for the sake of cache handling – James Jun 23 '16 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.