1

What's the canonical way to do a 301 redirect from any url which has www at the beginning to the root domain name - www.domain.com (or www.domain.com/something) to domain.com? And assuming there's also sub1.domain.com and, of course, there shouldn't be a redirect.

Is it before_filter in ApplicationController or something else?

Incerteza
  • 32,326
  • 47
  • 154
  • 261
  • you should define this in nginx, more info here: http://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-ww – mmln Dec 26 '14 at 13:59
  • @mymlyn, if I'm using something else, not nginx? – Incerteza Dec 26 '14 at 14:00
  • youre not using any http server in production? imo this exact redirection should be handled by your http server and not your application – mmln Dec 26 '14 at 14:34
  • @mymlyn, I'm. So redirection by web server is better than by application? – Incerteza Dec 26 '14 at 14:40

1 Answers1

1

You can simply check the request and do necessary redirections. You are right, it is done in application_controller.rb, something along these lines:

  before_filter :needs_redirection

  def needs_redirection
    redirect_to 'domain.com' if request.original_url.include? 'www.domain.com'
  end
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145