Topic: Redirect www.*.example.com to *.example.com?

Hi,

how can I redirect all request of the form "www.subdomain.example.com" to "subdomain.example.com"?

Background: I my case subdomain is the name of the client and is used in various places across the application and it would be a lot of work to consider "www.clientname" and "clientname" everywhere...

Thanks

Last edited by echinus (2012-03-20 17:13:56)

Re: Redirect www.*.example.com to *.example.com?

OK, meanwhile I have got this:

class SubdomainWww
  def self.matches?(request)  
    request.subdomain.start_with? "www."
  end
  
  def self.strip_www(subdomain)
    subdomain.slice!(4..-1) if subdomain.start_with? "www."
  end
end

And in Application.routes.draw do:

  constraints(SubdomainWww) do
    match '*path', :to => redirect(:subdomain => SubdomainWww.strip_www(??))  
    #match '/', :to => redirect(:subdomain => SubdomainWww.strip_www(??))  
  end

This might/should/does work fine. The only problem that is left is the question marks. I need to replace them with the subdomain of the current request; how do I do that?

Thanks

Re: Redirect www.*.example.com to *.example.com?

There is the rack-www gem and rack-no-www gem, depending on if you want those double-u's or not.

Learning rails, one day at a time.

http://keilmiller.com