1

I assume this is not really a difficult issue but I am using so many different solutions and don't really know what it best.

I am using Ruby on Rails and have my apps on Heroku and I want to 301 redirect everything on my naked domain (@) to my www-domain. E.g.

http://domain.com --> http://www.domain.com http://domain.com/subpage --> http://www.domain.com/subpage

Right now I am handling this with my DNS by first deleting both @ and www entries. Then I set a redirect of the entire website to http://www.domain.com (which re-creates the DNS entries for both @ and www). Lastly I change my www DNS-entry to CNAME and the name of the heroku-app (http://myapp.herokuapp.com).

This seems to be forwarding http://domain.com/subpage to http://www.domain.com (without the subpage).

What I am looking for now is the proper/recommended way to handle this in a simple/elegant way.

DNS? Routes? .htaccess? (if so, how do I alter .htaccess in RubyOnRails)

Thanks in advance!

Christoffer
  • 2,271
  • 3
  • 26
  • 57

4 Answers4

1

These questions may be related to your question, maybe have a look at those. It seems (from what I have read), that heroku does not allow access to anything like .htaccess or anything, so it seems like Rack middleware would be the best option.

Community
  • 1
  • 1
jvperrin
  • 3,368
  • 1
  • 23
  • 33
1

Where is your domain registered? If with GoDaddy, they offer a service that will handle the 301 redirect for you, but it requires that you sign up for one of their hosting plans. The lowest cost will do (~$5 per month). Not free, but painless and requires no coding, etc.

Chris Piazza
  • 251
  • 2
  • 6
1

(notice : did not try anything of this, but it should work)

One might argue that the nice way is to use the DNS or your webservers capabilities to do that. However, it is possible to do it with rails if you need it.

All in all, it has the advantage that you will easily keep any params / path in the process, as you told you wanted. Plus, the logic is inside the app and won't be lost if you need to scale up / change domain name. On the bad side, your full stack will be hit anytime someone uses the bare domain name.

you can try a before_filter in your application controller :

before_filter :redirect_to_www

def redirect_to_www
  redirect_to subdomain: 'www' unless request.subdomain == 'www'
end

if you want to avoid a "magic redirection" and make it clear for everyone, on rails 3 you can do this directly in the routes:

constraints ->(request){ request.subdomain != 'www' } do
  match '(*all)' => redirect( subdomain: 'www' )
end 

you should also add the subdomain to your default url options (application controller):

def default_url_options( option = {} )
  {subdomain: 'www'}
end
m_x
  • 12,357
  • 7
  • 46
  • 60
0

In the end what I had to do was to point the @-DNS to my normal web host (since @ demands an IP and Heroku will only accept CNAME) and the www-DNS to Heroku.

At my normal webhost I put a .htaccess that redirected all traffic there to my www-domain like this:

RewriteEngine On

### re-direct to www
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
Christoffer
  • 2,271
  • 3
  • 26
  • 57