Incerteza
Incerteza

Reputation: 34884

From www to the root domain (301)

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?

Upvotes: 1

Views: 69

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52357

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

Upvotes: 1

Related Questions