Rails does not serve these images, it lets the web server do that. You had best change the configuration of your web server to handle this scenario. If you use Apache
, for example, it would fairly easy to set up with mod_rewrite
.
Making Rails serve these images will be ugly, but it is possible if you provide a route in your routes.rb
that matches /public/images/unconventional.gif
, and if the file itself does not exist. For example:
map.connect "public/images/unconventional.gif",
:controller => "static_image_controller",
:action => "serve"
And then create a controller StaticImageController
:
class StaticImageController < ApplicationController
def serve
image = File.read(File.join(Rails.root, "unconventional.gif"))
send_data image, :type => "image/gif", :disposition => "inline"
end
end
Warning: if you use the above concept, note that if you use input from the URL to decide which file to serve (with params[:file]
, for example), you need to thoroughly sanitize the input, because you are risking exposing your entire file system to the outside world.