Thanks all. This was a great discussion.
In my opinion, editing the ".htaccess" file is the most elegant, direct
solution. However, given my stated phobia of editing server files, I
looked at all of your comments and ended up with this working solution:
class ApplicationController < ActionController::Base
before_filter :redirect_www
private
def redirect_www
begin
if request.host.include?("www.")
redirect_to(params.merge(:host=>request.host.sub('www.','')))
end
rescue
end
end
end
Some Notes --
1 I considered applying the filter only to my home page but I
occasionally have users who link to other pages so I added it to the
whole application even though it is a little more overhead (the filter
is applied to every request). Fortunately the overhead is whittled all
the way down to the boolean "if request.host.include?("www.")" which can
only be true once per session (the first page load).
2 I pre-emptively wrapped the code with "begin/rescue/end" because I've
encountered strange errors in the past where the request is nil and so
request.host would give a NoMethod error -- but, that's rare and this
may not be necessary here.
3 The solution does not apply to initial "https" requests because as
noted above the browser performs the certificate check prior to my
filter being able to substitute the "www.". Fortunately, my home page
is http so once the home page has applied the filter to
http://www.xyz.com all subsequent clicks on the site will omit the
"www."
Thanks for all your help -- this was a highly successful thread for me
and I hope it helps others. :)
Regards,
John Clancy
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---