[LRUG] FW: Redirects on heroku

Tom ten Thij mail at tomtenthij.nl
Wed Aug 4 08:31:42 PDT 2010


>     if request.env['HTTP_HOST'] != TheDomain
>       redirect_to 'http://' + TheDomain
>     end

This will blat the whole rest of the request, which would annoy people
who have bookmarked pages on your site. Rather, I would replace the
current hostname with the desired one and redirect to that. For
example, I've used this technique to redirect
http://www.war-logs-on-rails.com/term_tags/jingle-trucks to
http://afghanwarleak.org/term_tags/jingle-trucks.

Another thing I've used is heroku config settings:
    heroku config (gives you a list of current settings)
    heroku config:add DOMAIN_TO_REDIRECT_TO=afghanwarleak.org (sets a value)

This value can then be used with the following before filter:

  def ensure_domain
    if ENV["DOMAIN_TO_REDIRECT_TO"] && request.env['HTTP_HOST'] !=
ENV["DOMAIN_TO_REDIRECT_TO"]
      redirect_to request.url.sub(request.env['HTTP_HOST'],
ENV["DOMAIN_TO_REDIRECT_TO"])
    end
  end

Which of course would be cleaner to do in middleware as others have
suggested. This also allows you to configure the hostname on the fly
and prevents the redirect from happening in the development/test
environment.

Tom.

-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
 Tom ten Thij
 Unboxed Consulting, http://unboxedconsulting.com
 T: +44 20 3137 2943 F: +44 20 7183 4251
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~



More information about the Chat mailing list