Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure Apache to handle missing image assets?

All the image assets in my Rails application live in /public/images and are served by Apache if they exist on the server. If a request for a missing image is made, Apache can't serve it so it gets passed on to Rails which subsequently raises a 404.

Ideally I would like any request for a missing image to be handled at the Apache level, rather than be forwarded on to Rails. How best can I achieve this?

like image 405
Olly Avatar asked Nov 05 '22 12:11

Olly


1 Answers

Redirecting with mod_rewrite to a 404 page if the directory and file do not exist.

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule .? /404.html [L]

Here, -f matches an existing filename and -d matches an existing directory name. That will check to see that the requested filename is not an existing filename or directory name before it redirects to the 404 page (or whatever you like).

like image 141
Slobodan Kovacevic Avatar answered Nov 11 '22 03:11

Slobodan Kovacevic