0

我最近在 Heroku 上升级到新的 cedar 堆栈时遇到了问题。因此,我通过将旧网站转储到由下面的 sinatra 代码驱动的静态公共文件夹中来解决这个问题。

但是,旧 url 的链接不会加载静态页面,因为它们无法将 .html 附加到 url 的末尾。

require 'rubygems'
require 'sinatra'

set :public, Proc.new { File.join(root, "public") }

before do
  response.headers['Cache-Control'] = 'public, max-age=100' # 5 mins
end

get '/' do
  File.read('public/index.html')
end

如何将 .html 附加到所有网址的末尾?会是这样的吗:

get '/*' do
  redirect ('/*' + '.html')
end
4

1 回答 1

1

您可以通过params[:splat]helper 或从 helper获取匹配的路径request.path_info,我倾向于使用第二个:

get '/*' do
  path = params[:splat].first # you've only got one match
  path = "/#{path}.html" unless path.end_with? ".html" # notice the slash here!
  # or
  path = request.path_info
  path = "#{path}.html" unless path.end_with? ".html" # this has the slash already
  # then
  redirect path
end
于 2012-11-09T12:43:08.403 回答