0

最近、Herokuの新しい杉スタックへのアップグレードで問題が発生しました。そこで、古いWebサイトを、以下のsinatraコードを使用する静的なパブリックフォルダーにダンプすることで、この問題を回避しました。

ただし、古いURLへのリンクは、URLの末尾に.htmlを追加できないため、静的ページをロードしません。

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

すべてのURLの末尾に.htmlを追加するにはどうすればよいですか?これは次のようになりますか?

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

1 に答える 1

1

params[:splat]またはヘルパーから一致するパスを取得できますrequest.path_info。私は2番目を使用する傾向があります。

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 に答える