I have a Rails+Apache+Passenger setup and my app serves wildcard subdomains. I need all www URLs to redirect to their non www equivalents.
My current vhost config is as below
<VirtualHost *:80>
ServerName example.net
ServerAlias *.example.net
DocumentRoot /home/public_html/example.net/current/public
RailsEnv staging
</VirtualHost>
I tried an assortment of rewrite rules in various locations but none took effect. I've checked to make sure that the apache rewrite module is enabled and RewriteEngine is on. Not sure what I'm missing. All help much appreciated!
I solved this issue in my app, as I have logic based on the domain anyway. Place this code in your ApplicationController
class ApplicationController < ActionController::Base
before_filter :check_host
def check_host
if request.host.split('.')[0] == 'www'
redirect_to "http://" + request.host.gsub('www.','')
end
end
end
Could have special cases if some of your hostnames contain "www." for any other reason that you'd have to code for.