3
votes

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?

3
It really is strange. I've checked the snippet against half a dozen of our Heroku/Cedar/Rails app which uses this code and it is exactly correct. Most odd!John Beynon
Am I missing something, like a "require rack-rewrite" in some file?David
nope, nothing like that. It would be loaded automatically being as it's in the gemfile.John Beynon
Try logging, log the env stuff, and the rack_env['SERVER_NAME'] env['REQUEST_URI'] and the result of your check. I think on heroku it's just puts to log.sunkencity
If you have an index.html in public then that is what is being served and not your Rails application - just like when you forget to delete the template one when you run rails newJohn Beynon

3 Answers

5
votes

change

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

to

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

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/
0
votes

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.