It is a simple issue, but I can\'t seem to find an answer doing some quick googling.
What\'s the Ruby on Rails way of doing this 301 direct (http://x.com/abc > http:
Ideally you'd do this in your web server (Apache, nginx etc.) configuation so that the request doesn't even touch Rails at all.
Add the following before_filter
to your ApplicationController
:
class ApplicationController < ActionController::Base
before_filter :add_www_subdomain
private
def add_www_subdomain
unless /^www/.match(request.host)
redirect_to("#{request.protocol}x.com#{request.request_uri}",
:status => 301)
end
end
end
If you did want to do the redirect using Apache, you could use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.x\.com [NC]
RewriteRule ^(.*)$ http://www.x.com/$1 [R=301,L]