I'm serving a static site (generated by Jekyll/Octopress) on Heroku, using the thin webserver and a basic Sinatra app. My goal is to support case-insensitive URLs (either by serving the same page from URLs that vary by case, or by redirect/rewrite).
E.g. http:/mysite/Hello.html
and http://mysite/hello.html
should both serve the same file.
Here's the config.ru
(from Octopress distrib):
require 'bundler/setup'
require 'sinatra/base'
require 'rack'
require 'rack/rewrite'
# The project root directory
$root = ::File.dirname(__FILE__)
class SinatraStaticServer < Sinatra::Base
get(/.+/) do
send_sinatra_file(request.path) {404}
end
not_found do
send_sinatra_file('404.html') {"Sorry, I cannot find #{request.path}"}
end
def send_sinatra_file(path, &missing_file_block)
file_path = File.join(File.dirname(__FILE__), 'public', path)
file_path = File.join(file_path, 'index.html') unless file_path =~ /\.[a-z]+$/i
File.exist?(file_path) ? send_file(file_path) : missing_file_block.call
end
end
run SinatraStaticServer
Is there a way to do this using Rack or Sinatra? So far the furthest I've got is down-casing all files and using .downcase
in send_sinatra_file
.
Here is an idea for your routing:
get '/' do
send_file(File.join(File.dirname(__FILE__), 'public', 'index.html'))
end
get '*' do
file_path = File.join(File.dirname(__FILE__), 'public', request.path.downcase)
File.exist?(file_path) ? send_file(file_path) : halt 404
end
not_found do
send_file(File.join(File.dirname(__FILE__), 'public', '404.html'))
end
This is untested, since I probably won't be able to reproduce your environment anyways. But I think that it would work for your purpose, assuming all your pages are lowercase (as you mentioned) and that you also have an actual 404.html
page.