使用不区分大小写的 URL 从 Sinatra 提供静态文件

发布于 2025-01-03 03:15:59 字数 1151 浏览 2 评论 0原文

我正在 Heroku 上使用瘦网络服务器和基本的 Sinatra 应用程序提供静态站点(由 Jekyll/Octopress 生成)。我的目标是支持不区分大小写的 URL(通过从大小写不同的 URL 提供同一页面,或者通过重定向/重写)。

例如 http:/mysite/Hello.htmlhttp://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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不如归去 2025-01-10 03:15:59

这是您的路由的一个想法:

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

这是未经测试的,因为我可能无论如何都无法重现您的环境。但我认为它可以满足您的目的,假设您的所有页面都是小写的(正如您所提到的)并且您还有一个实际的 404.html 页面。

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.

复古式 2025-01-10 03:15:59

我刚刚使用了 gem https://github.com/substancelab/route_downcaser。非常容易使用。它将把 sinatra 接收到的所有路由转换为小写。

从文档中:

只需安装 gem
gem install route_downcaser

加载Sinatra后使用

require 'route_downcaser'
use RouteDowncaser::DowncaseRouteMiddleware

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

require 'route_downcaser'
use RouteDowncaser::DowncaseRouteMiddleware
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文