views:

625

answers:

3

I noticed that Sinatra does not recognize index.html files in public folder's subdirectories and returns an error when url is pointing to a directory without specifiying the file name.

For example, if user enters a url like "www.mydomain.com/subdiretory/", Sinatra fails to recognize the existence of an index.html file in that directory.

There are hundreds of subdirectories in my public folder so that it is impossible to specify each one of them in code (and the number of subdirectories keeps growing).

How can I tell Sinatra to leave my web server (Apache) alone (to server index.html file) if there is an index.html file in a subdirectory of public folder when url is pointing to that directory without the file name?

A: 

What about this solution? :

get "/subdirectory/:file" do 
  file = params[:file] + "index.html"
  if File.exists?(params[:file])
    return File.open("subdirectory/" + file)
  else
   return "error"
  end
end

so if you now navigate to (for example) /subdirectory/test/ it will load subdirectory/test/index.html

+2  A: 

sinatra should let you serve static files from the public directory

from the docs:

Static Files Static files are served from the ./public directory. You can specify a different location by setting the :public option: Note that the public directory name is not included in the URL. A file ./public/css/style.css is made available as example.com/css/style.css.

http://www.sinatrarb.com/intro.html

imightbeinatree at Cloudspace
A: 

This question has been answered excellently and succinctly here.

action_ben