Question

I have a simple rack app:

map "/foo" do
  use Rack::Static, 
    :urls => [""], :root => 'public', :index => 'admin.html'
  run lambda {|env| [200, {}, []]}
end

When I browse to lvho.st/foo I get 404 File not Found but if I go to lvho.st/foo/admin.html it's loaded just fine.

I want it to serve admin.html at lvho.st/foo

What am I doing wrong?

Thanks!

Was it helpful?

Solution

You might have whitnessed yourself that even though lvho.st/foo might not work, lvho.st/foo/ (with a trailing slash) however does! So the question is how to either skip checking for the trailing slash or append it to all requests. The problem can be solved with a small .htaccess redirect on a Passenger / Apache server, but we're in Ruby, so here's the Ruby solution - the rack-slashenforce gem.

Run this in your project folder (or append it to your Gemfile and run bundle install):

gem install rack-slashenforce

Then your Ruby file would look like this:

require 'rack-slashenforce'

# Make sure you declare this outside the map block
use Rack::AppendTrailingSlash
map "/foo" do
  use Rack::Static, :urls => [""], :root => 'public', :index => 'admin.html'
  run lambda {|env| [200, {}, []]}
end

Now every call to lvho.st/foo will get a slash appended! And everything should work fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top