3

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?

David
  • 213
  • 4
  • 10
  • 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 Jan 12 '12 at 08:54
  • 1
    Am I missing something, like a "require rack-rewrite" in some file? – David Jan 12 '12 at 09:04
  • nope, nothing like that. It would be loaded automatically being as it's in the gemfile. – John Beynon Jan 12 '12 at 09:18
  • 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 Jan 12 '12 at 10:59
  • I'm pretty new to ruby/rails, honestly I'll admit I have no idea how to do that. – David Jan 12 '12 at 11:48
  • just to show that my sample does actually work on heroku - visit http://stark-beach-5145.herokuapp.com and you'll be immediately redirected since the domain doesn't match kyan.com - github project is https://github.com/johnbeynon/rackrewritetest - entirely vanilla Rails 3.1 with rackrewrite and the initializer. – John Beynon Jan 12 '12 at 12:26
  • If I load MyAppName.herokuapp.com it doesn't do anything...other than load what should be there – David Jan 12 '12 at 13:22
  • I think I've found a lead! Since I'm serving a single page (index.html) with a /style folder for it's CSS...if I remove the index.html page, all of a sudden the Rewrite works. – David Jan 12 '12 at 14:31
  • 1
    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 new` – John Beynon Jan 12 '12 at 22:43

3 Answers3

5

change

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

to

rack_env['SERVER_NAME'] == 'domain.com'
concept47
  • 30,257
  • 12
  • 52
  • 74
0

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.

Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
Elisha
  • 63
  • 6
0

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/
sunkencity
  • 3,482
  • 1
  • 22
  • 19
  • I'm not sure if you're suggesting for me to change something? – David Jan 12 '12 at 08:47
  • I think you should look in 'REQUEST_URI' instead of 'SERVER_NAME' for this rewrite, this is because SERVER_NAME might not actually be www.foobar.com but rather the internal name of the server behind the proxy. It's a proxied setup at heroku. – sunkencity Jan 12 '12 at 08:57
  • So I should change to rack_env['REQUEST_URI'] != 'www.domain.com' instead of rack_env['SERVER_NAME'] != 'www.domain.com' – David Jan 12 '12 at 09:01
  • No, you should not use !=, request URI is the full URI. Use a regexp, like in my example. – sunkencity Jan 12 '12 at 09:45
  • 1
    so `rack_env['REQUEST_URI'] !~ /www.domain.com/` = no go, as was `env['REQUEST_URI'] !~ /www.domain.com/` in case that's what you meant – David Jan 12 '12 at 10:04
  • This did not help me... in fact it gave my application errors and wouldn't even load. – courtsimas Jan 18 '12 at 23:34