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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With