23

How do I redirect http://vinderhimlen.dk to http://www.vinderhimlen.dk ?

Rails beginner
  • 14,321
  • 35
  • 137
  • 257
  • Not to be to picky or anything but konkurrance is spelled konkurrence. It appears to be spelled both ways on the page... – HakonB Mar 22 '11 at 10:14
  • possible duplicate of [How do I redirect domains on heroku?](http://stackoverflow.com/questions/5045107/how-do-i-redirect-domains-on-heroku) – John Topley Mar 22 '11 at 10:35

7 Answers7

36

Your best bet would be to set up redirect with your DNS provider, so it happens long before any request reaches your server. From the Heroku Dev Center:

Subdomain redirection results in a 301 permanent redirect to the specified subdomain for all requests to the naked domain so all current and future requests are properly routed and the full www hostname is displayed in the user’s location field.

DNSimple provides a convenient URL redirect seen here redirecting from the heroku-sslendpoint.com naked domain to the www.heroku-sslendpoint.com subdomain. enter image description here

For proper configuration on Heroku the www subdomain should then be a CNAME record reference to yourappname.herokuapp.com.

It's not just DNSimple that does this. My DNS provider is 123 Reg and they support it but call it web forwarding.

Mike
  • 9,692
  • 6
  • 44
  • 61
  • Thanks a lot for your comment - I happened to be using DNSSimple so it worked like a charm for me! – Jean Barmash Sep 27 '12 at 13:06
  • 1
    It may be worth noting that the other way around (redirecting all `www.` requests to naked domain) also works the same way. – Mladen Jablanović Jan 30 '13 at 12:38
  • 3
    You should be aware that sometimes, when dealing with SSL, this kind of redirect won't do it. Specifically, DNSimple cannot deal with a redirect like https://example.com => https://www.example.com. See http://support.dnsimple.com/articles/url-redirect-ssl – Jesper Aug 25 '13 at 16:54
10

I would recommend to do that much earlier in the request-lifecycle. If you would use Apache, you would add the URL Rewrite to the VirtualHosts file. On Heroku you need to add some Rack middleware.

Here as an example of the other way round (i.e. www.example.org -> example.org). I don't think you will have big problems of changing it to your usecase.

This way the requests won't show up in your log and don't need all of Rails' request parsing.

John Topley
  • 113,588
  • 46
  • 195
  • 237
Marcel Jackwerth
  • 53,948
  • 9
  • 74
  • 88
4

My solution:

Terminal:

heroku addons:add custom_domains:basic
heroku domains:add www.vinderhimlen.dk
heroku domains:add vinderhimlen.dk

And then (http://devcenter.heroku.com/articles/custom-domains):

class ApplicationController
  before_filter :ensure_domain

  APP_DOMAIN = 'www.vinderhimlen.dk'

  def ensure_domain
    if request.env['HTTP_HOST'] != APP_DOMAIN
      # HTTP 301 is a "permanent" redirect
      redirect_to "http://#{APP_DOMAIN}", :status => 301
    end
  end
end
Rails beginner
  • 14,321
  • 35
  • 137
  • 257
  • 2
    if you also want to keep contents of request after the host: redirect_to "http://#{APP_DOMAIN}#{request.env['REQUEST_PATH']}", :status => 301 – LennonR Feb 28 '12 at 19:10
  • 1
    it's better to use rack middleware for that. for example http://blog.dynamic50.com/2011/02/22/redirect-all-requests-for-www-to-root-domain-with-heroku/ – Nikolay Mar 12 '12 at 11:35
  • 1
    Much better to do this with DNS. – Rails beginner Jan 27 '14 at 21:53
2

Assuming you are using an action controller, just use redirect_to:

redirect_to "http://www.vinderhimlen.dk"
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
1

You should add the www domain as well.

heroku domains:add www.vinderhimlen.dk

Fareesh Vijayarangam
  • 5,037
  • 4
  • 23
  • 18
1

Normally you'd do this at the HTTP server level using something like Apache's mod_rewrite module so that it occurs before the request even reaches the Rails' stack. However, Heroku don't give you access to their HTTP server configuration, so an alternative is needed.

I'd recommend taking a look at Refraction, which is Rack middleware designed to replace mod_rewrite. It lets you write your rewrite rules using good old readable Ruby code and it's still faster than using Rails itself for the task.

John Topley
  • 113,588
  • 46
  • 195
  • 237
0

See Justice's answer in this SO article - his method for this is clear, simple, effective, and customizable.

Community
  • 1
  • 1
Will Ayd
  • 6,767
  • 2
  • 36
  • 39