I've got a Rails+Apache+Passenger setup and my application serves wildcard subdomains. I want all www Web addresses to redirect for their non www counterparts.

  • world wide web.example.internet should redirect to example.internet
  • world wide web.subdomain.example.internet should redirect to subdomain.example.internet

My current vhost config is really as below

<VirtualHost *:80>

  ServerName  example.net
  ServerAlias *.example.net

  DocumentRoot /home/public_html/example.net/current/public

  RailsEnv staging

</VirtualHost>

I attempted a variety of rewrite rules in a variety of locations but none of them required effect. I have checked to make certain the apache rewrite module is enabled and RewriteEngine is on. Unsure what I am missing. All help much appreciated!

You should use moderewrite inside your .htaccess file.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.net [NC]
RewriteRule ^(.*)$ http://example.net/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^www\.subdomain\.example\.net [NC]
RewriteRule ^(.*)$ http://subdomain.example.net/$1 [R=301,NC]

This will operate in however i not test drive it.
or this

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

I solved this problem during my application, when i have logic in line with the domain anyway. Place this code inside 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

Might have special cases if a number of your hostnames contain "www." for just about any other reason why you'd need to code for.