ruby-on-rails-3 - 在没有 .htaccess 的情况下在 Heroku 上托管的 Rails3 中强制 'www'

标签 ruby-on-rails-3 routing rack

我想知道是否有 Rack 替代方案来强制 URL 中的“www”,因为 Heroku 不使用 .htaccess 文件。

也许甚至是在 route 做到这一点的好方法?

谢谢

最佳答案

在您的 ApplicationController 中,您可以简单地创建一个 before 过滤器:

before_filter :force_www!

protected

def force_www!
  if Rails.env.production? and request.host[0..3] != "www."
    redirect_to "#{request.protocol}www.#{request.host_with_port}#{request.fullpath}", :status => 301
  end
end

或者转到另一个方向并删除 www:
before_filter :remove_www!

protected

def remove_www!
  if Rails.env.production? and request.host[0..3] == "www."
    redirect_to "#{request.protocol}#{request.host_with_port[4..-1]}#{request.fullpath}", :status => 301
  end
end

关于ruby-on-rails-3 - 在没有 .htaccess 的情况下在 Heroku 上托管的 Rails3 中强制 'www',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6905664/

相关文章:

ruby-on-rails - 如何对按日期时间排序的记录进行排序?

macos - 如何以编程方式重新路由来自特定进程的所有 TCP/IP 或 HTTP 连接

heroku - Foreman:在开发和生产中使用不同的Procfile

ruby-on-rails - 使用 Apache2 和 Passenger 跟踪 Rack gem

ruby-on-rails-3 - 测试整个 Rspec 套件失败

javascript - Kendo UI 网格 - 更新不持久

node.js - 重新调整可选路由参数

asp.net-mvc - ASP.Net MVC 路由策略

ruby-on-rails - 什么是 Ruby 中基于 Rack 的服务器

ruby-on-rails - 如何将设计中的默认 json 响应更改为自定义响应?