Posts Tagged ‘apache’

Removing the www on a passenger rails app

February 19th, 2009

I’ve recently moved from nginx with mongrels to phusion passenger on apache.

I like to drop the www subdomain on my sites when I can, but the passenger chaps recommend that you don’t use mod_rewrite with mod_rails (aka mod_rack or even mod_ruby).

So how’s it to be done?

The simple solution is to do the re-write in rails. I found this snippet which does the trick (thanks to RSL):

def trim_www
  if request.subdomains.first == "www"
    if request.subdomains == ["www"]
      redirect_to "#{request.protocol}#{request.domain}#{request.port_string}#{request.path}"
    else
      subdomains = request.subdomains.shift.join(".")
      subdomains << "." unless subdomains.blank?
      redirect_to "#{request.protocol}#{subdomains}#{request.domain}#{request.port_string}#{request.path}"
    end
  end
end

http://gist.github.com/67043