如何在 Heroku 上使用 Ruby Rack 为静态站点设置 URL [英] How to setup URLs for static site with Ruby Rack on Heroku

查看:35
本文介绍了如何在 Heroku 上使用 Ruby Rack 为静态站点设置 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站在这里.

它曾经是一个由 Django 驱动的博客.但是我不再更新它,所以我只是想让它成为一个静态的 HTML 站点.我使用 Ruby Rack 将其修改并移至 Heroku.

It used to be a Django-powered blog. However I no longer update it so I just wanted to make it a static HTML site. I wget'ed it and moved it to Heroku with Ruby Rack.

但是,每个 URL 都解析为主页.这是因为我的 config.ru 文件:

However every URL resolves to the home page. This is because of my config.ru file:

use Rack::Static, 
  :urls => ["/media/images", "/media/js", "/media/css"],
  :root => "public"

run lambda { |env|
  [
    200, 
    {
      'Content-Type'  => 'text/html', 
      'Cache-Control' => 'public, max-age=86400' 
    },
    File.open('public/index.html', File::RDONLY)
  ]
}

问题:有没有办法映射多个 URL?例如foo.com/about 映射到 public/about/index.html, foo.com/posts/2012/oct/21/blog-post>映射到public/posts/2012/oct/21/blog-post/index.html

Question: Is there a way to map multiple URLs? e.g. foo.com/about maps to public/about/index.html, foo.com/posts/2012/oct/21/blog-postmaps to public/posts/2012/oct/21/blog-post/index.html

在这一点上,我什至可以手动打字.

At this point I'd even be fine typing each one by hand.

感谢您的帮助.

推荐答案

目前我找到的最佳答案是:

For now I found the best answer to be:

use Rack::Static, 
  :urls => ["/media/images", "/media/js", "/media/css"],
  :root => "public"

map "/" do
  run lambda { |env|
  [
    200, 
    {
      'Content-Type'  => 'text/html', 
      'Cache-Control' => 'public, max-age=86400' 
    },
    File.open('public/index.html', File::RDONLY)
  ]
}
end

map "/portfolio" do
  run lambda { |env|
  [
    200, 
    {
      'Content-Type'  => 'text/html', 
      'Cache-Control' => 'public, max-age=86400' 
    },
    File.open('public/portfolio/index.html', File::RDONLY)
  ]
}
end

并将每个 URL 映射到其各自的文件.乏味,但有效.另请参阅有关 URL 变量的问题的答案.但无法让它为我工作.

And map every URL to its respective file. Tedious, but works. See also the answer to this question regarding URL variables. Couldn't get it to work for me though.

这篇关于如何在 Heroku 上使用 Ruby Rack 为静态站点设置 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆