Heroku + Rack-Rewrite

一笑奈何 提交于 2020-01-02 08:44:11

问题


Still can't get this working...Rails 3.1.3, Ruby 1.9.2 on Heroku's Cedar Stack.

Trying to use https://github.com/jtrupiano/rack-rewrite to make http://domain 301 redirect to http://www.domain to no luck (app works, but no redirects happen at all).

/config/initializers/rack_rewrite.rb (MyAppName is actually the correct name, domain.com is actual domain):

MyAppName::Application.config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
    r301 %r{.*}, 'http://www.domain.com$&', :if => Proc.new {|rack_env|
    rack_env['SERVER_NAME'] != 'www.domain.com'
    }
end

Added to Gemfile:

gem 'rack-rewrite'

Did "gem install rack-rewrite", "bundle install".

No luck.

Any ideas?

UPDATE:

I have figured out PART of the problem. Since I'm just trying to serve "index.html" and it's "/style" folder, it appears that having "index.html" in "/public" overrides the rack-rewrite. If I remove "index.html", the rewrites work...but now I don't know where to put the files, or set up the routes.rb to direct to the index.html page by default...any help?


回答1:


change

rack_env['SERVER_NAME'] != 'www.domain.com'

to

rack_env['SERVER_NAME'] == 'domain.com'



回答2:


I think that maybe env["SERVER_NAME"] could be an internal dns in this case like 'app7009.intra.foo'. I do some stuff with domains in middleware in heroku: I look at both env['REQUEST_URI'] and env['PATH_INFO'], mainly because the POW-server I use locally doesn't set REQUEST_URI. It's a bit different how different servers populate the env hash, I wish this URL request part would be more standard with something like rack.

env['REQUEST_URI'] !~ /www.domain.com/



回答3:


I'm new to this so I have no logical explanation as to why it works but it worked for me when I put the codes in config/application.rb instead of a new file /config/initializers/rack_rewrite.rb.



来源:https://stackoverflow.com/questions/8831785/heroku-rack-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!