How do I redirect http://vinderhimlen.dk to http://www.vinderhimlen.dk ?
-
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 Answers
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 thewww.heroku-sslendpoint.com
subdomain.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
.

- 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
-
1It 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
-
3You 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
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.

- 113,588
- 46
- 195
- 237

- 53,948
- 9
- 74
- 88
-
2
-
@John: That's why lines 2-5 explain how to do it on Heroku with middleware. – Marcel Jackwerth Mar 22 '11 at 10:40
-
1I like this middleware: https://github.com/tylerhunt/rack-canonical-host Just set the domain including `www.` as canonical. And `whatever-you-use.herokuapp.com` will redirect there too with no extra effort. – Henrik N Sep 07 '12 at 20:43
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

- 14,321
- 35
- 137
- 257
-
2if 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
-
1it'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
Assuming you are using an action controller, just use redirect_to
:
redirect_to "http://www.vinderhimlen.dk"

- 176,835
- 32
- 241
- 292
-
-
I have added redirect_to "http://www.vinderhimlen.dk" to my application controller still see this message Heroku | No such app – Rails beginner Mar 22 '11 at 10:16
-
You need to add that www-less hostname to Heroku. http://devcenter.heroku.com/articles/custom-domains – Marcel Jackwerth Mar 22 '11 at 10:20
You should add the www domain as well.
heroku domains:add www.vinderhimlen.dk

- 5,037
- 4
- 23
- 18
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.

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