2012-11-09 3 views
0

최근 Heroku의 새 삼나무 스택으로 업그레이드하는 데 문제가있었습니다. 그래서 나는 아래의 sinatra 코드에 의해 구동되는 정적 인 공용 폴더에 나의 오래된 웹 사이트를 덤핑함으로써 해결했다.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 

어떻게하면 모든 URL의 끝에 .html을 추가 할 수 있습니까?

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

답변

2

당신은 어느 params[:splat] 통해 또는 도우미 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 
+0

위대한는이 같은 것! 치료를해라! 많은 감사. – user251732