使用不区分大小写的 URL 从 Sinatra 提供静态文件
我正在 Heroku 上使用瘦网络服务器和基本的 Sinatra 应用程序提供静态站点(由 Jekyll/Octopress 生成)。我的目标是支持不区分大小写的 URL(通过从大小写不同的 URL 提供同一页面,或者通过重定向/重写)。
例如 http:/mysite/Hello.html
和 http://mysite/hello.html
应该提供相同的文件。
这是 config.ru
(来自 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
有没有办法使用机架还是西纳特拉?到目前为止,我所能做到的最远的就是缩小所有文件的大小并在 send_sinatra_file
中使用 .downcase
。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您的路由的一个想法:
这是未经测试的,因为我可能无论如何都无法重现您的环境。但我认为它可以满足您的目的,假设您的所有页面都是小写的(正如您所提到的)并且您还有一个实际的
404.html
页面。Here is an idea for your routing:
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.我刚刚使用了 gem https://github.com/substancelab/route_downcaser。非常容易使用。它将把 sinatra 接收到的所有路由转换为小写。
从文档中:
只需安装 gem
gem install route_downcaser
加载Sinatra后使用
I just used the gem https://github.com/substancelab/route_downcaser. Very easy to use. It will convert all the routes sinatra receives into lowercase.
From the docs:
Just install gem
gem install route_downcaser
and then use after loading Sinatra